Python - print (출력)

닉네임유저·2023년 7월 31일

Python - 기초 문법

목록 보기
6/13
post-thumbnail

print - 화면에 텍스트를 출력하는 데 사용되는 기본적인 함수


프로그래밍 좀 한다 하면 , 그 어디에서나 보는 기초적인 print 문

JAVA - println
C++ - printf
C# - printf
Python - print


선언 : print()

print("Hello, World!")

변수와 함께 사용하기

# 변수와 함께 사용하기
item = '아이스크림'
quantitiy = 10
print('item : ', item, 'quantitiy : ', quantitiy)

문자열 포맷팅 .format() : 보기 좋은 형태로 출력하기

# 문자열 포맷 .format
item = '아이스크림'
quantitiy = 10
print('item : {} ', 'quantitiy : {} '.format(item, quantitiy))

f-string (Python 3.6 부터 추가)

# f-string (Python 3.6 version 이상 부터 사용가능)
item = '아이스크림'
quantitiy = 10
print(f'item : {item}, quantitiy : {quantitiy} ')

end=' ' 매개변수 줄 바꿈 대신 공백 지정

print("Hello", end=" ")
print("World!")

문자열 중간에 값을 삽입

name = "User"
age = 20
print("My name is {} and I am {} years old.".format(name, age))

Escape

# 이스케이프 문자사용
print("이것은 큰 따옴표(\")입니다.")
print("이것은 탭 문자(\t)를 포함하는 문장입니다.")
print("이것은 역슬래시(\\)를 출력하는 예제입니다.")

sep 매개변수 사용하기 : 출력되는 값 사이에 원하는 문자 삽입

a = 10
b = 15
c = 20

print(a,b,c,sep=':')

출력 방향 지정하기 - 리눅스/유닉스


python my_script.py > output.txt  # 파일로 출력
python my_script.py | another_program  # 파이프로 출력
profile
이것저것 다해보는 개발자

0개의 댓글