Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

돌맹이

[Python] 문자열 포매팅(String Formatting) 본문

programming/Python

[Python] 문자열 포매팅(String Formatting)

오택 2022. 12. 27. 16:41

문자열 포매팅이란 문자열 안에 특정 값 또는 변수를 삽입하기 위한 방법이다.

문자열 포매팅 방식은 3가지다.

 

1. %기호 방식

2. format 메서드 방식

3. f 문자열 방식

 

예시)

month = 12, temperature = -5
print("12월 평균 온도는 -5도 입니다")
print("%d월 평균 온도는 %d도 입니다" %(month, temperature)) # %방식
print("{}월 평균 온도는 {}도 입니다".format(month, temperature)) # format 메서드 방식
print(f"{month}월 평균 온도는 {temperature}도 입니다") # f문자열 방식

 

 

'programming > Python' 카테고리의 다른 글

[Python] NumPy(넘파이)  (0) 2022.12.28
[Python] Docstring(문서화)  (0) 2022.12.27
[Python] Dictionary 자료형  (0) 2022.12.27
[Phython] Pythonic: 파이썬 문법  (0) 2022.12.26