https://docs.python.org/3/tutorial/inputoutput.html#the-string-format-method
str.format() 요약
- 기본 형태:
'We are the {} who say "{}!"'.format('knights', 'Ni')
# {} 자리에 순서대로 치환- 위치 인덱스:
'{0} and {1}'.format('spam', 'eggs')
# 순서 바꾸기 가능
'{1} and {0}'.format('spam', 'eggs')- 키워드:
'This {food} is {adjective}.'.format(food='spam', adjective='absolutely horrible')- 위치+키워드 혼용:
'The story of {0}, {1}, and {other}.'.format('Bill', 'Manfred', other='Georg')- 딕셔너리 사용:
table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
'{0[Jack]:d}'.format(table)
# str.format()의 사용법으로 특이한 케이스 딕셔너리에서 키값을
# 처럼 0은 첫 번째 인자, [...]로 키 접근. 혹은 format(**table)로 키워드처럼 풀어 쓰기.- vars() 활용:
- 로컬 변수를 딕셔너리로 받아 format(**vars()) 패턴에 사용 가능.
- 폭/정렬 포맷:
- ‘{0:2d} {1:3d} {2:4d}‘.format(x, xx, xx*x)처럼 :폭 지정해 열 맞추기; d는 정수 형식.