Python Basic _ 1. print

WONY_yoon·2025년 10월 3일
post-thumbnail

Print 사용법

# chapter01-1
# print 사용법

# 기본 출력
print('Python Start!')      # Python Start!
print("python start!")      # python start!
print('''python start!''')  # python start!
print("""python start!""")  # python start!

print()

# separator 옵션
print('p', 'y', 't', 'h', 'o', 'n', sep='♡')   # p♡y♡t♡h♡o♡n
print('p', 'y', 't', 'h', 'o', 'n', sep='|')   # p|y|t|h|o|n
print('010', '7702', '6630', sep='-')          # 010-7702-6630
print('python', 'google.com', sep='@')         # python@google.com

print()

# end 옵션
print('welcome to', end=' ')
print('IT News', end=' ')
print('web site')    # welcome to IT News web site

print()

# file 옵션
import sys
print('Learn Python', file=sys.stdout)   # Learn Python

print()

# format 사용(d-정수, s-문자열, f-실수)
print('%s %s' % ('one', 'two'))            # one two
print('{} {}'.format('one', 'two'))        # one two
print('{1} {0}'.format('one', 'two'))      # two one

print()

# %s
print('%10s' % ('nice'))                   #       nice
print('{:>10}'.format('nice'))             #       nice

print('%-10s' % ('nice'))                  # nice      
print('{:10}'.format('nice'))              # nice      

print('{:☆>10}'.format('nice'))            # ☆☆☆☆nice
print('{:^10}'.format('nice'))             #   nice   

print('%.5s' % ('nice'))                   # nice
print('%.5s' % ('pythonstudy'))            # pytho
print('%5s' % ('pythonstudy'))             # pythonstudy
print('{:10.5}'.format('pythonstudy'))     # pytho     

print()

# %d
print('%d %d' % (1, 2))                    # 1 2
print('{} {}'.format(1, 2))                # 1 2

print('%4d' % (42345342))                  # 42345342
print('%4d' % (42))                        #   42
print('{:4d}'.format(42345342))            # 42345342
print('{:4d}'.format(42))                  #   42

print()

# %f
print('%f' % (3.1234567890))               # 3.123457
print('{:f}'.format(3.1234567890))         # 3.123457
print('%1.8f' % (3.1234567890))            # 3.12345679

print('%06.2f' % (3.1234567890))           # 003.12
print('{:06.2f}'.format(3.1234567890))     # 003.12

Printing Formatting

# chapter02-2
# print 사용법
# 파이썬 3가지 print formatting

"""
참고 : Escape 코드

\n : 개행
\t : 탭
\\ : 문자
\' : 문자
\" : 문자
\000 : 널 문자
"""

# 3가지 format practices

x = 50
y = 100
text = 30583134
n = 'Lee'

# 출력 1 (% formatting)
ex1 = 'n = %s, s = %s, sum = %d' % (n, text, (x + y))
print(ex1)    
# n = Lee, s = 30583134, sum = 150

# 출력 2 (str.format)
ex2 = 'n = {n}, s = {s}, sum = {sum}'.format(n=n, s=text, sum=x + y)
print(ex2)    
# n = Lee, s = 30583134, sum = 150

# 출력 3 (f-string)
ex3 = f'n = {n}, s = {text}, sum = {x + y}'
print(ex3)    
# n = Lee, s = 30583134, sum = 150

print(f'n = {n}, s = {text}, sum = {x + y}')  
# n = Lee, s = 30583134, sum = 150

print()

# 구분기호
n = 1000000000000
print(f'n : {n:,}')     # n : 1,000,000,000,000

print()

# 정렬
# ^ : 가운데 정렬 / < : 왼쪽정렬 / > : 오른쪽정렬
t = 20

print(f't : {t:10}')      # t :         20
print(f't center : {t:^10}')   # t center :    20     
print(f't center : {t:<10}')   # t center : 20        
print(f't center : {t:>10}')   # t center :         20

print()

print(f't center : {t:-^10}')  # t center : ---20-----
print(f't center : {t:#<10}')  # t center : 20########

0개의 댓글