원하는 문자열로 출력하기 위해 사용하는 함수
data1 = 10
data2 = 10.4562
print("data1 : {}".format(data1))
print("data1 : {}\ndata2 : {}".format(data1, data2))
출력 자리수를 지정
서식문자의 사이에 정수를 넣어 자릿수 확보, 정렬을 한다.
음수면 좌측정렬, 양수면 우측정렬
print("|%5d|123"%(123)) # | 123|123
print("|%-5d|123"%(123)) # |123 |123
height = 165.9888
print("|%.2f|" %height) # |165.99|
또 다른 정렬 방법
print("|{:>5}|".format(123)) # | 123| 우측정렬
print("|{:<5}|".format(123)) # |123 | 좌측정렬
print("|{:^5}|".format(123)) # | 123 | 가운데정렬
print("|{:5}|".format(123)) # | 123| 디폴트
print("|{:<15.2f}|".format(123.456))
여백을 문자로 채우는 방법
print("|%05d|123" %(123)); # |00123|123
print("|{:0>5}|".format(123)) # |00123|
print("|{:_<5}|".format(123)) # |123__|
print("|{:%^5}|".format(123)) # |%123%|
print("|{:%^15.2f}|".format(123.456)) # |%%%%123.46%%%%%|
print("{:,}".format(1000000)) # 1,000,000
print("{:,.2f}".format(1000000)) # 1,000,000.00
뭔가 헷갈린다.