a, b, c = 25, 75, 100
print(a, ' plus ', b, ' equals ', c, '.')
25 plus 75 equals 100 .
print(a, ' plus ', b, ' equals ', c, '.', sep='')
25 plus 75 equals 100.
문자열 포맷팅 연산자 % 사용법
값이 1개일 때
포맷_문자열 % 값
값이 여러 개일 때
포맷_문자열 % (여러 값들)
print('%d plus %d equals %d.' % (a, b, c))
25 plus 75 equals 100.
'Here is a number: %d.' % 100
'Here is a number: 100.'
n = 25 + 75
fmt_str = 'The sum is %d.'
print(fmt_str % n)
The sum is 100.
n, m = 100, 200
print('n is %d' % n)
print('n is %d and m is %d' % (n, m))
n is 100 n is 100 and m is 200
tup = 10, 20, 30
print('Answers are %d, %d, and %d.' % tup)
Answers are 10, 20, and 30.
h1 = int('e9', 16)
h2 = int('10', 16)
print('The result is %x.' % (h1 + h2))
The result is f9.
print('The result is %x.' % h1 + h2) # 에러!
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /var/folders/fb/55f4mk_d67x6jtxmmtsn_hjh0000gn/T/ipykernel_48057/1543535552.py in <module> ----> 1 print('The result is %x.' % h1 + h2) # 에러! TypeError: can only concatenate str (not "int") to str
print('The result is 0x%x.' % (h1 + h2))
The result is 0xf9.
s = 'We is %s, %s, & %s.' % ('Moe', 'Curly', 'Larry')
print(s)
We is Moe, Curly, & Larry.
포멧팅 양식
%[-][너비][.정밀도]c
'This is a number: %-6d.' % 255
'This is a number: 255 .'
print('My name is %10s.' % 'John')
My name is John.
print('Amount is %10d.' % 25)
print('Amount is %.5d.' % 25)
print('Amount is %10.5d.' % 25)
Amount is 25. Amount is 00025. Amount is 00025.
print('result:%12.5f' % 3.14)
print('result:%12.5f' % 333.14)
result: 3.14000 result: 333.14000
print('%.4f' % 3.141592)
3.1416
print('%.2f' % 3.141592)
3.14
'The number is %s.' % 10
'The number is 10.'
'The number is %r.' % 10
'The number is 10.'
print('My name is %r.' % 'Sam')
My name is 'Sam'.
print('My name is %s.' % 'Sam')
My name is Sam.
print(str('대한민국'))
대한민국
print(repr('대한민국'))
'대한민국'
'Here is a number: %*d' % (3, 6)
'Here is a number: 6'
'Here is a number: %*s' % (3, 'VI')
'Here is a number: VI'
'Item 1: %*s, Item 2: %*s' % (8, 'Bob', 8, 'Suzanne')
'Item 1: Bob, Item 2: Suzanne'
n = 8
'Item 1: %*s, Item 2: %*s' % (n, 'Bob', n, 'Suzanne')
'Item 1: Bob, Item 2: Suzanne'
n = 9
'Item 1: %*r, Item 2: %*r' % (n, 'Bob', n, 'Suzanne')
"Item 1: 'Bob', Item 2: 'Suzanne'"
'%*.*f' % (8, 3, 3.141592)
' 3.142'
big_n = 10 ** 12
format(big_n, ',')
'1,000,000,000,000'
format(데이터, 사양/양식/포멧 지시자/포멧 명세)
너비: 최소 길이를 나타냄
쉼표 기호(,): 천 단위 위치 구분자
정밀도: 출력할 부동소수점 숫자의 자리수
타입 문자:
사실은 다음이 더 완벽한 명세
[[채우기]자리 맞춤][기호][#][0][너비][,][.정밀도][타입_문자]
n = 150000000
print(format(n, ','))
150,000,000
print(format("대한민국", ','))
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-18-872260c53618> in <module> ----> 1 print(format("대한민국", ',')) ValueError: Cannot specify ',' with 's'.
format('Bob', '10')
'Bob '
format('Suzie', '7')
'Suzie '
format(150, '8')
' 150'
format(99, '5')
' 99'
format("대한민국", '2')
'대한민국'
format('Bobby K.', '6.3')
'Bob '
format(3.141592, '6.3')
' 3.14'
format(3.141592, '6.3f')
' 3.142'
format(3.141592, '9.3f')
' 3.142'
format(100.7, '9.3f')
' 100.700'
print('{} plus {} equals {}.'.format(25, 75, 100))
25 plus 75 equals 100.
fss = '{} said, I want {} slices of {}.'
name = 'Pythagoras'
pi = 3.141592
print(fss.format(name, 2, pi))
Pythagoras said, I want 2 slices of 3.141592.
print('Set = {{{}, {}}}'.format(1, 2))
Set = {1, 2}
fss = 'Set = {{ {}, {}, {} }}'
print(fss.format(15, 35, 25))
Set = { 15, 35, 25 }
print('Set = {{ {}, {}, {} }}'.format(15, 35, 25))
Set = { 15, 35, 25 }
print('{}; {}; {}!'.format(10, 20, 30))
10; 20; 30!
print('{0}; {1}; {2}!'.format(10, 20, 30))
10; 20; 30!
print('The items are {2}, {1}, {0}.'.format(10, 20, 30))
The items are 30, 20, 10.
fss = 'The items are {3}, {1}, {0}.'
print(fss.format(10, 20, 30, 40))
The items are 40, 20, 10.
fss = 'a equals {a}, b equals {b}, c equals {c}.'
print(fss.format(a=10, c=100, b=50))
a equals 10, b equals 50, c equals 100.
print('{0}, {0}, {1}, {1}'.format(100, 200))
100, 100, 200, 200
current_lang = input('한국어를 원하면 KRW를 입력하세요:')
if current_lang == 'KRW':
fss = '{0}가 {2}에서 {1}을 만나는 게 언제지?'
else:
fss = "When will {0} meet {1} at {2}'s?"
print(fss.format('Fred', 'Sam', 'Joe'))
한국어를 원하면 KRW를 입력하세요:KRW Fred가 Joe에서 Sam을 만나는 게 언제지?
a_list = [100, 200, 300]
'{0[1]:}, {0[2]:}'.format(a_list)
'200, 300'
'{a[1]:}, {a[2]:}'.format(a=a_list)
'200, 300'
문자열 내부의 포멧 문자열 양식
{ [위치] [!r|s|a] [:사양] }
[[채우기]자리 맞춤][기호][#][0][너비][,][.정밀도][타입_문자]
print(10) # 10 출력
print(str(10)) # 동일한 결과!
10 10
print(repr(10)) # 이 출력 결과도 10이다.
10
test_str = 'Here is a \n newline! '
print(test_str)
Here is a newline!
print(repr(test_str))
'Here is a \n newline! '
print('{}'.format(test_str))
Here is a newline!
print('{!r}'.format(test_str))
'Here is a \n newline! '
print('{1!r} loves {0!r}'.format('Joanie', 'ChaCha'))
'ChaCha' loves 'Joanie'
[[채우기]자리 맞춤][기호][#][0][너비][,][.정밀도][타입_문자]
n1, n2 = 777, 999
print('**{:10}**{:2}**'.format(n1, n2))
** 777**999**
n_lst = [111, 10, 2001, 2, 55, 144, 2525, 1984]
for n in n_lst:
print('{:5}'.format(n))
111 10 2001 2 55 144 2525 1984
fss = '{1!r:10} loves {0!r:10}!!'
print(fss.format('Joanie', 'ChaCha'))
'ChaCha' loves 'Joanie' !!
[[채우기]자리 맞춤][기호][#][0][너비][,][.정밀도][타입_문자]
print('{:->24}'.format('Hey Bill G, pick me!'))
----Hey Bill G, pick me!
print('{:>7}'.format('Tom')) # ' Tom' 출력
print('{:@>7}'.format('Lady')) # '@@@Lady' 출력
print('{:*>7}'.format('Bill')) # '***Bill' 출력
Tom @@@Lady ***Bill
print('{:<7}'.format('Tom')) # 'Tom ' 출력
print('{:@<7}'.format('Lady')) # 'Lady@@@' 출력
print('{:*<7}'.format('Bill')) # 'Bill***' 출력
Tom Lady@@@ Bill***
fss = '{:^10}Jones'
print(fss.format('Tom')) # ' Tom Jones' 출력
fss = '{:@^10}'
print(fss.format('Lady')) # '@@@Lady@@@' 출력
fss = '{:*^10}'
print(fss.format('Bill')) # '***Bill***' 출력
Tom Jones @@@Lady@@@ ***Bill***
print('{:=8}'.format(-1250)) # '- 1250' 출력
print('{:0=8}'.format(-1250)) # '-0001250' 출력
- 1250 -0001250
print(format('Lady', '@<7')) # 'Lady@@@' 출력
Lady@@@
print('{:@<7}'.format('Lady')) # 'Lady@@@' 출력
Lady@@@
[[채우기]자리 맞춤][기호][#][0][너비][,][.정밀도][타입_문자]
print('results> {:}, {:+}, {:-}'.format(25, 25, 25))
results> 25, +25, 25
print('results> {:}, {:+}, {:-}'.format(-25, -25, -25))
results> -25, -25, -25
[[채우기]자리 맞춤][기호][#][0][너비][,][.정밀도][타입_문자]
i, j = 125, 25156
print('{:07} {:010}'.format(i, j))
0000125 0000025156
print('{:08}'.format(375))
00000375
i, j = 125, 25156
print('{:0>7} {:0>10}'.format(i, j))
0000125 0000025156
print('{:0>+10} {:+010}'.format(25, 25))
0000000+25 +000000025
fss1 = 'The USA owes {:,} dollars.'
print(fss1.format(21000000000))
fss2 = 'The sun is {:,} miles away.'
print(fss2.format(93000000))
The USA owes 21,000,000,000 dollars. The sun is 93,000,000 miles away.
n = 4500000
print('The amount on the check was ${:*>12,}'.format(n))
The amount on the check was $***4,500,000
print('The amount is {:011,}'.format(13000))
The amount is 000,013,000
n = 13000
print('The amount is {:012,}'.format(n))
The amount is 0,000,013,000
print('The amount is {:0>11,}'.format(n))
The amount is 0000013,000
[[채우기]자리 맞춤][기호][#][0][너비][,][.정밀도][타입_문자]
pi = 3.14159265
phi = 1.618
fss = '{:.2} + {:.2} = {:.2}'
print(fss.format(pi, phi, pi + phi))
3.1 + 1.6 = 4.8
pi = 3.14159265
phi = 1.618
fss = '{:.3} + {:.3} = {:.3}'
print(fss.format(pi, phi, pi + phi))
3.14 + 1.62 = 4.76
pi = 3.14159265
phi = 1.618
fss = '{:.3f} + {:.3f} = {:.3f}'
print(fss.format(pi, phi, pi + phi))
3.142 + 1.618 = 4.760
fss = ' {:10.3f}\n {:10.3f}'
print(fss.format(22.1, 1000.007))
22.100 1000.007
fss = ' {:10,.3f}\n {:10,.3f}'
print(fss.format(22333.1, 1000.007))
22,333.100 1,000.007
fss = ' {:10.2f}'
for x in [22.7, 3.1415, 555.5, 29, 1010.013]:
print(fss.format(x))
22.70 3.14 555.50 29.00 1010.01
[[채우기]자리 맞춤][기호][#][0][너비][,][.정밀도][타입_문자]
print('{:.5}'.format('Superannuated.')) # 'Super' 출력
print('{:.5}'.format('Excellent!')) # 'Excel' 출력
print('{:.5}'.format('Sam')) # 'Sam' 출력
Super Excel Sam
fss = '{:*<6.6}'
print(fss.format('Tom'))
print(fss.format('Mike'))
print(fss.format('Rodney'))
print(fss.format('Hannibal'))
print(fss.format('Mortimer'))
Tom*** Mike** Rodney Hannib Mortim
print(format('Tom', '*<6.6'))
print(format('Mike', '*<6.6'))
print(format('Rodney', '*<6.6'))
print(format('Hannibal', '*<6.6'))
print(format('Mortimer', '*<6.6'))
Tom*** Mike** Rodney Hannib Mortim
fss = '{:*<5.10}'
print(fss.format('Tom'))
print(fss.format('Rodney'))
print(fss.format('longer than 10'))
Tom** Rodney longer tha
[[채우기]자리 맞춤][기호][#][0][너비][,][.정밀도][타입_문자]
print('{:b} {:b} {:b}'.format(5, 6, 16))
101 110 10000
print('{:#b}'.format(7))
0b111
print('{:o}, {:x}, {:X}'.format(63, 63, 63))
77, 3f, 3F
print('{0:o}, {0:x}, {0:X}'.format(63))
77, 3f, 3F
print('{0:#o}, {0:#x}, {0:#X}'.format(63))
0o77, 0x3f, 0X3F
print('You own {:%} of the shares.'.format(.517))
You own 51.700000% of the shares.
print('You own {:8%} of the shares.'.format(.517))
You own 51.700000% of the shares.
print('{:.2%} of {:.2%} of 40...'.format(0.231, 0.5))
23.10% of 50.00% of 40...
def calc_binary():
print('Enter values in binary only!')
b1 = int(input('Enter b1:'), 2)
b2 = int(input('Enter b2:'), 2)
print('Total is: {:#b}'.format(b1 + b2))
print('{} + {} = {}'.format(b1, b2, b1 + b2))
calc_binary()
Enter values in binary only! Enter b1:101 Enter b2:1010 Total is: 0b1111 5 + 10 = 15
'Here is a num: {:{}.{}}'.format(1.2345, 10, 4)
'Here is a num: 1.234'
'Here is a num: {:10.4}'.format(1.2345)
'Here is a num: 1.234'
a, b = 10, 4
'Here is a num: {:{}.{}}'.format(1.2345, a, b)
'Here is a num: 1.234'
'{:{}} {:{}}!'.format('Hi', 3, 'there', 7)
'Hi there !'
'{0:{1}} {2:{3}}!'.format('Hi', 3, 'there', 7)
'Hi there !'
'Pi is approx. {0:{1}.{2}f}'.format(3.141592, 8, 3)
'Pi is approx. 3.142'
a, b = 8, 3
'Pi is approx. {0:{1}.{2}f}'.format(3.141592, a, b)
'Pi is approx. 3.142'
'Pi is approx. {0:8.3f}'.format(3.141592)
'Pi is approx. 3.142'
for i in range(2, 10):
for j in range(1, 10):
print("{0} x {1} = {2:2d}".format(i, j, i*j))
print()
2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24 3 x 9 = 27 4 x 1 = 4 4 x 2 = 8 4 x 3 = 12 4 x 4 = 16 4 x 5 = 20 4 x 6 = 24 4 x 7 = 28 4 x 8 = 32 4 x 9 = 36 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 6 x 1 = 6 6 x 2 = 12 6 x 3 = 18 6 x 4 = 24 6 x 5 = 30 6 x 6 = 36 6 x 7 = 42 6 x 8 = 48 6 x 9 = 54 7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 8 x 1 = 8 8 x 2 = 16 8 x 3 = 24 8 x 4 = 32 8 x 5 = 40 8 x 6 = 48 8 x 7 = 56 8 x 8 = 64 8 x 9 = 72 9 x 1 = 9 9 x 2 = 18 9 x 3 = 27 9 x 4 = 36 9 x 5 = 45 9 x 6 = 54 9 x 7 = 63 9 x 8 = 72 9 x 9 = 81
for el in range(10):
print(el, 'inch =', el * 2.54, 'centi')
0 inch = 0.0 centi 1 inch = 2.54 centi 2 inch = 5.08 centi 3 inch = 7.62 centi 4 inch = 10.16 centi 5 inch = 12.7 centi 6 inch = 15.24 centi 7 inch = 17.78 centi 8 inch = 20.32 centi 9 inch = 22.86 centi
for el in range(10):
print("%d inch = %.2f centi" % (el, el * 2.54))
0 inch = 0.00 centi 1 inch = 2.54 centi 2 inch = 5.08 centi 3 inch = 7.62 centi 4 inch = 10.16 centi 5 inch = 12.70 centi 6 inch = 15.24 centi 7 inch = 17.78 centi 8 inch = 20.32 centi 9 inch = 22.86 centi
for el in range(10):
print("{0} inch = {1:.2f} centi".format(el, el * 2.54))
0 inch = 0.00 centi 1 inch = 2.54 centi 2 inch = 5.08 centi 3 inch = 7.62 centi 4 inch = 10.16 centi 5 inch = 12.70 centi 6 inch = 15.24 centi 7 inch = 17.78 centi 8 inch = 20.32 centi 9 inch = 22.86 centi