문자열 포맷팅

특정 포맷에 맞추어 문자열을 재배치하는 것

% 연산자와 str.format 사용

ex 1 )

height = 172.5

a = '키는 %.2f입니다.' % height

ex 2 )

name = '김수영'

a = '나는 %s입니다.' % name

ex 3)숫자 앞에 0을 채워야 하는 경우

year = 2020

month = 3

day = 5

a = '%d-%02d-%02d' % (year, month, day)

ex4)

name = '이진선'

a = '이름 : {}'.format(name)

='이름 : {}'.format("이진선")

---

-format() 메소드 (Method) 이용

항목이 두 개 이상일 경우에는 각 항목을 콤마(,)로 구분

Last updated

Was this helpful?