Python/Python 기초 정리
[Python] print문 옵션 정리
Jake the Dog
2022. 3. 14. 10:36
반응형
기본 출력 방식
# 기본 출력
print('Hello Python!') # 문법적 중요
print("Hello Python!") # 텍스트 의미
print("""Hello Python!""")
print('''Hello Python!''')
separator 옵션 사용
- 콤마(,)로 구분된 문자는 공백으로 연결되어 출력되며, 이 부분을 sep= 옵션을 통해 다른 문자로 바꾸어 줄 수 있다.
# separator 옵션 사용
print('T', 'E', 'S', 'T', sep='')
print('2019', '02', '19', sep='-')
print('niceman', 'google.com', sep='@')
end 옵션 사용
- 기본적으로 개행문자가 들어었고 end옵션으로 print문의 끝 부분을 지정해 줄 수 있다.
# end 옵션 사용
print('Welcome To', end=' ')
print('the black parade', end=' ')
print('piano notes')
file 옵션 사용
# file 옵션 사용
import sys
print('GeeksForGeeks', file=sys.stdout)
format 사용
# format 사용
print('{} and {}'.format('You', 'Me'))
print('{0} and {1} and {0}'.format('You', 'Me'))
print('{var1} are {var2}'.format(var1='You', var2='Niceman'))
%d, %f, %s 옵션 사용
# %d, %f, %s
print("Test1: %5d, Price: %4.2f" % (776, 6534.123))
print("Test1: {0:5d}, Price:{1:4.2f}".format(776, 6534.123))
print("Test1: {a: 5d}, Price:{b: 4.2f}".format(a=776, b=6534.123))
반응형