반짝이는 오렌지

[Python] 기초문법: print() 함수 사용방법 본문

Python/Python 기초

[Python] 기초문법: print() 함수 사용방법

twinkle orange 2022. 2. 19. 22:44
728x90
반응형

print() 함수는 화면에 결과물을 출력하기 위한 함수이다.

함수 입력방법이 궁금하다면 help() 함수를 사용해보자.

help(print)

Help on built-in function print in module builtins: print(...)

print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.

Optional keyword arguments:

file: a file-like object (stream); defaults to the current sys.stdout.

sep: string inserted between values, default a space.

end: string appended after the last value, default a newline.

flush: whether to forcibly flush the stream.

 

 

print() 함수안에 value를 여러개 입력할 수 잇고, sep, end, file, flush 등을 옵션으로 설정할 수 있다.

print() 함수안에 여러 3개의 문자열을 입력하면 문자열 그대로 출력이 된다.

이 때, 작은따옴표 안에 있는 문자열만 출력되고 작은따옴표는 출력되지 않는 것을 확인할 수 있다.

print('You Need Python','a','123')
# You Need Python a 123

이 때, 문자열 세 개 사이에 구분자를 넣고 싶다면 sep을 활용하면 된다.

sep = ','을 추가하면 output에 콤마(,)가 문자열 사이에 추가되어 출력된다.

print('You Need Python','a','123',sep = ',')
# You Need Python,a,123

print() 함수에 각각 하나의 문자열을 입력하면 한줄에 하나의 print가 출력이 된다.

print('You Need Python' )
print('a')
print('123')
#You Need Python
#a
#123

만약 각 print 함수안에 있는 입력값을 특정 조건으로 연결하고 싶다면 end 를 활용하여 다음과 같이 입력할 수 있다.

print('You Need Python' , end = ' ')
print('a')
print('123')
#You Need Python a
#123

첫 번재 함수 print('You Need Python' , end = ' ') 에 end=' ' 를 입력했다. 스페이스를 포함한 문자열을 입력했다.

print() 함수의 끝을 스페이스로 마무리 하는 것을 의미하며, 다음 print() 함수의 결과물을 스페이스로 연결해준다.

end 의 경우 default 값이 end='\n' 이다.

'\n'은 이스케이프코드로 enter를 의미하며, 각 print() 함수의 결과물을 enter로 연결한다.

 

이스케이프코드는 출력물을 보기 좋게  정리하는 코드로 다음 게시글에서 정리하겠다.

반응형