4.9.4. 문자열 포맷 연산자
print "My name is %s and weight is %d kg!" % ('Zara', 21)Last updated
Was this helpful?
Was this helpful?
sub1 = "python string!"
sub2 = "an arg"
a = "i am a %s" % sub1
b = "i am a {0}".format(sub1)
c = "with %(kwarg)s!" % {'kwarg':sub2}
d = "with {kwarg}!".format(kwarg=sub2)
print(a) # "i am a python string!"
print(b) # "i am a python string!"
print(c) # "with an arg!"
print(d) # "with an arg!"
# 위치를 기준으로 한 포맷팅
print('We are the {} who say "{}!"'.format('student', 'children'))
print('We are the {0} who say "{1}!"'.format('student', 'children'))
# 필드명을 기준으로 한 포맷팅
print("Age of student {name} is {age}".format(age=29, name='James'))
# object의 인덱스 혹은 키를 사용하여 포맷팅
pos = [12.5, 35, 90]
print("A의 좌표는 x = {p[0]}, y = {p[1]}, z = {p[2]}".format(p=pos))