' '
, " "
, ''' '''
, """ """
등을 통해서 원하는 입력을 출력할 수 있다.
print('Hello Python!')
print("Hello Python!")
print("""Hello Python!""")
print('''Hello Python!''')
# 출력결과
# Hello Python!
# Hello Python!
# Hello Python!
# Hello Python!
Separator 옵션은 python에서 ,
를 기준으로 자동으로 공백을 넣는데 이에 대하여 원하는 값을 넣을 수 있다.
print('T', 'E', 'S', 'T')
print('T', 'E', 'S', 'T', sep='')
print('2020', '10', '17', sep='-')
print('niceman', 'google.com', sep='@')
# 출력결과
# T E S T
# TEST
# 2020-10-17
# niceman@google.com
end 옵션은 python에서 print 함수는 default로 개행을 처리해준다. 하지만 이 옵션을 통해 개행을 삭제하거나 공백으로 처리할 수 있다.
print('Welcome To', end='')
print('the black parade', end='')
print(' piano notes', end='')
# 출력결과
# Welcome Tothe black parade piano notes
format 옵션은 입력에 원하는 부분에 {}
를 넣어서 이에 대하여 원하는 값이나 변수를 넣어서 출력할 수 있다. {}
에는 배열처럼 순서대로 0부터 시작해서 값을 넣어도 출력할 수 있고 format에 해당하는 값에 변수명을 지정하여 이를 매칭해서 값을 출력할 수도 있다.
print('{} and {}'.format('You', 'Me'))
print('{0} and {1} and {0}'.format('You', 'Me'))
print('{a} and {b} and {a}'.format(a='You', b='Me'))
# 출력결과
You and Me
You and Me and You
You and Me and You
%s
는 문자, %d
는 정수, %f
는 실수로 이를 중간에 format의 {}
처럼 집어넣어서 출력의 원하는 부분에 변수를 넣어서 출력할 수 있다.
print("%s's favorite number is %d" % ('JinHo', 7))
print("Test1: %5d, Price: %4.2f" % (776, 6534.123)))
# 출력결과
#JinHo's favorite number is 7
#Test1: 776, Price: 6534.12
print 출력을 할 때 유용한 코드
참고 : Escape 코드
\n
: 개행
\t
: 탭
\\
: 문자
\'
: 문자
\"
: 문자
\r
: 캐리지 리턴
\f
: 폼 피드
\a
: 벨 소리
\b
: 백 스페이스
\000
: 널 문자
print("'you'")
print('\'you\'')
print("""'you'""")
print('\\you\\')
print('\ttest')
# 출력결과
#'you'
#'you'
#'you'
#\you\
# test