상세 컨텐츠

본문 제목

0214TIL - Python print

Python/Today I Learned

by lucar 2025. 2. 14. 21:22

본문


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

print()

#Separator 옵션
#print 문장의 마지막에 sep 옵션을 추가하여 여러 항목을 출력할 때에 그 사이에 추가될 문자나 문장을 추가할 수 있다.
print('P','Y','T','H','O','N', sep = " ")
print('010','7777','1234', sep='-')
print('python', 'google.com', sep = '@')

print()

#end 옵션
#print문장의 마지막에 end 옵션을 추가하여 줄의 마지막 부분을 어떻게 처리할지 설정할 수 있다.
print('Welcome to', end='')
print('IT News', end='')
print('Web Site')

#file 옵션
#file 옵션을 통해 출력 값을 외부의 파일에 저장할 수 있다.
import sys

print('Learn Python', file = sys.stdout)

#format 사용(d, s, f)
#d = 정수(int) s = 문자열(string) f = 실수(float)
print('%s %s' % ("one", "two"))

print('{} {}'.format('one', 'two')) #format함수를 사용하여 var처럼 자동으로 변수형을 지정해 줄 수 있음
print('{1}, {0}'.format('one', 'two')) #숫자를 입력함으로 format함수의 변수가 삽입되는 순서를 정해줄 수 있음

# %s
print('%10s' % ('nice')) # %와 변수형의 사이에 숫자를 입력하여 최소 자릿 수를 설정가능
print('{:>10}'.format('nice')) # 위와 같은 결과가 출력 됨

print('%-10s' % ('nice')) #-를 달거나
print('{:10}'.format('nice')) #부등호를 없앰으로 좌측부터 출력가능

print('{:_>10}'.format('nice')) #콜론과 숫자의 사이에 추가되는 문자를 해당 자리에 출력함 이 경우에는 언더바_

print('{:^10}'.format('nice')) # ^기호를 입력하여 중간 정렬 가능

print('%.5s' % ('nice'))
print('%.5s' % ('python study')) #.을 사용하여 공간을 제한가능 .5를 입력하여 5글자만 출력됨

print('{:10.5}'.format('pythonstudy')) # 10.5를 입력하는 경우에는 10개의 자리중에 5글자만 출력


관련글 더보기