garret

[Python] sys.stdout.write()와 print() 차이 본문

Programming/Python

[Python] sys.stdout.write()와 print() 차이

_Sun_ 2023. 5. 23. 17:16

백준 알고리즘 문제를 풀면서 알게된 sys.stdout.write()함수.

출력함수인데 print() 비슷한 듯 다른 것 같아 차이점을 정리해보았다.

 

 

sys.stdout 정의

used for the output of print() and expression statements and for the prompts of input(): 

 

 

 

 

sys.stdout.write()와 print의 차이

 

  출력 형태
sys.stdout.write() 줄바꿈없이 이어서 출력
print() 줄바꿈하여 출력

 

 

 

 

sys.stdout.write 출력 예시

import sys
for i in range(5):
    sys.stdout.write(f"{i}")

 

※ 주의사항 ※

sys.stdout.write는 string만 넣을 수 있다. 

 

 

str말고 다른 자료형을 넣으면 TypeError 발생하니 주의!

 

 

 

print 출력 예시

for i in range(5):
    print(i)

 

 

 

Reference

 

sys — System-specific parameters and functions

This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It is always available. Citations C99, ISO/IEC 9899...

docs.python.org

 

 

[기특공부] sys.stdout.write( ) (feat. 기특공부의 시작)

샛길 공부에 이어 기특공부를 시작하였다. 기특하게도 샛길인 아닌 찾아서 한 공부를 뜻하는 '기특공부' 첫 발걸음을 떼본다! sys.stdin을 공부하면서 standard in이 있으면 out도 있지 않을까? 란 생각

coding-nurse.tistory.com