본문 바로가기
SW 프로그래밍/파이썬

print함수, format함수 사용법

by N2info 2019. 12. 3.
>>> 
>>> print('파이썬(python) print함수, format함수 굉장히 복합해요')
파이썬(python) print함수, format함수 굉장히 복합해요
>>> 
>>> print('자주 사용하는 핵심만 복습합니다.')
자주 사용하는 핵심만 복습합니다.
>>> 
>>> print('암기하려고 하지 말고 따라 하면서 이해해 보세요')
암기하려고 하지 말고 따라 하면서 이해해 보세요
>>> 
>>> print('print함수, format함수')
print함수, format함수

>>> a = 10
>>> b = 20
>>> c = 30
>>> 
>>> print(' a = {}, b = {}, c = {} '.format(a, b, c))
 a = 10, b = 20, c = 30 
>>> 
>>> print(' a = {0}, b = {1}, c = {2} '.format(a, b, c))
 a = 10, b = 20, c = 30 
>>> 
>>> print(' a = {0}, b = {0}, c = {0} '.format(a, b, c))
 a = 10, b = 10, c = 10 
>>> 
>>> a = 10
>>> b = 20
>>> c = 30
>>> 
>>> print('a = [{}],  b = [{}], c = [{}]' .format(a, b, c))
a = [10],  b = [20], c = [30]
>>> 
>>> print('a = [{0:5d}],  b = [{1:10d}], c = [{2:20d}]' 
     .format(a, b, c))
a = [   10],  b = [        20], c = [                  30]
>>> 
>>> print('a = [{0:>5d}],  b = [{1:>10d}], c = [{2:>20d}]' 
     .format(a, b, c))
a = [   10],  b = [        20], c = [                  30]
>>> 
>>> print('a = [{0:<5d}],  b = [{1:<10d}], c = [{2:<20d}]'  \
             .format(a, b, c))
a = [10   ],  b = [20        ], c = [30                  ]
>>> 
>>> print('생각을 해 보세요')
생각을 해 보세요
>>> 
>>> print('[{0:<5d}], [{1:<20d}]' .format(100, 200))
[100  ], [200                 ]
>>> 
>>> print('[{0:>5d}], [{1:>20d}]' .format(100, 200))
[  100], [                 200]
>>> 
>>> print('[{0:^5d}], [{1:^20d}]' .format(100, 200))
[ 100 ], [        200         ]
>>> 
>>> print('천천히 각을 해 보세요')
천천히 각을 해 보세요
>>> 
>>> 
>>> a = 'apple'
>>> b = 'banana'
>>> 
>>> a, b
('apple', 'banana')
>>> 
>>> print(a, b)
apple banana
>>> 
>>> print('a => {},  b => {}' .format(a, b))
a => apple,  b => banana
>>> 
>>> print('a => {0},  b => {1}' .format(a, b))
a => apple,  b => banana
>>> 
>>> print('a => {0},  b => {0}' .format(a, b))
a => apple,  b => apple
>>> 
>>> print('a => {1},  b => {1}' .format(a, b))
a => banana,  b => banana
>>> 
>>> print('눈 코딩 말고, 손 코딩 하세요')
눈 코딩 말고, 손 코딩 하세요
>>> 
>>> a = 'pineapple'
>>> a
'pineapple'
>>> print(' a ==> [{0:20s}] '.format(a))
 a ==> [pineapple           ] 
>>> 
>>> print(' a ==> [{0:-20s}] '.format(a))
Traceback (most recent call last):
  File "<pyshell#215>", line 1, in <module>
    print(' a ==> [{0:-20s}] '.format(a))
ValueError: Sign not allowed in string format specifier
>>> print(' a ==> [{0:+20s}] '.format(a))
Traceback (most recent call last):
  File "<pyshell#216>", line 1, in <module>
    print(' a ==> [{0:+20s}] '.format(a))
ValueError: Sign not allowed in string format specifier
>>> print('일부러 에러 내 봤어요. C언어 스타일 안 됩니다.')
일부러 에러 내 봤어요. C언어 스타일 안 됩니다.
>>> 
>>> print(' a ==> [{0:<20s}] '.format(a))
 a ==> [pineapple           ] 
>>> print(' a ==> [{0:>20s}] '.format(a))
 a ==> [           pineapple] 
>>> print(' a ==> [{0:^20s}] '.format(a))
 a ==> [     pineapple      ] 
>>> 
>>> 
>>> print('실수(float) 출력합니다')
실수(float) 출력합니다
>>> 
>>> 
>>> a = 123.4567
>>> b = 3.141592
>>> 
>>> print('a ==> {}, b ==> {}' .format(a, b))
a ==> 123.4567, b ==> 3.141592
>>> 
>>> print('a ==> {0}, b ==> {2}' .format(a, b))
Traceback (most recent call last):
  File "<pyshell#243>", line 1, in <module>
    print('a ==> {0}, b ==> {2}' .format(a, b))
IndexError: tuple index out of range
>>> 
>>> print('a ==> {0}, b ==> {1}' .format(a, b))
a ==> 123.4567, b ==> 3.141592
>>> 
>>> print('a ==> {0}, b ==> {0}' .format(a, b))
a ==> 123.4567, b ==> 123.4567
>>> print('실수(float) 출력합니다')
실수(float) 출력합니다
>>> 
>>> print('a ==> {0:f}, b ==> {1:f}' .format(a, b))
a ==> 123.456700, b ==> 3.141592
>>> 
>>> print('a ==> [{0:10.10f}], b ==> [{1:3.7f}]' .format(a, b))
a ==> [123.4567000000], b ==> [3.1415920]
>>> 
>>> print('a ==> [{0:20.20f}], b ==> [{1:10.2f}]' .format(a, b))
a ==> [123.45669999999999788542], b ==> [      3.14]
>>> 
>>> a, b
(123.4567, 3.141592)
>>> 
>>> print('a ==> [{0:1.5f}], b ==> [{1:.5f}]' .format(a, b))
a ==> [123.45670], b ==> [3.14159]
>>> 
>>> print('a ==> [{0:.5f}], b ==> [{1:.5f}]' .format(a, b))
a ==> [123.45670], b ==> [3.14159]
>>> 
>>> print('a ==> [{0:.10f}], b ==> [{1:.20f}]' .format(a, b))
a ==> [123.4567000000], b ==> [3.14159200000000016217]
>>> 
>>> print('a ==> [{0:<.10f}], b ==> [{1:<.20f}]' .format(a, b))
a ==> [123.4567000000], b ==> [3.14159200000000016217]
>>> print('a ==> [{0:>.10f}], b ==> [{1:>.20f}]' .format(a, b))
a ==> [123.4567000000], b ==> [3.14159200000000016217]
>>> print('a ==> [{0:^.10f}], b ==> [{1:^.20f}]' .format(a, b))
a ==> [123.4567000000], b ==> [3.14159200000000016217]
>>> 
>>> print('정수, 문자열에서 사용하던 >, <, ^은 안 통하죠.')
정수, 문자열에서 사용하던 >, <, ^은 안 통하죠.
>>> 
>>> print(' 이 정도면 기본은 충분합니다.' )
 이 정도면 기본은 충분합니다.
>>> 

 

출처 : https://m.blog.naver.com/PostView.nhn?blogId=ahalinux&logNo=221091871442&proxyReferer=https%3A%2F%2Fwww.google.com%2F