Data Analysis/Codility
[python] 문자열 자료형 (1) f "~~{ }", .format('x'), %d, %s
[지현]
2021. 1. 8. 11:30
편리하다고 생각되는 순서대로 정리해두었다. 3.6버전 이상부터 사용가능한 f "~~{변수명}"이 제일 편하다고 느껴진다.
다만 자릿수를 고정할 때 다음과 같이 쓴다.
name = "Eric"
age = 74
temp = f"Hello, {name}. You are {age :03d}."
>> 'Hello, Eric. You are 074.'
#0. f "~~{ }"
1
2
3
4
5
6
7
8
9
|
name = '지방이'
b = f"adf {name} asdf."
print(b)
adf 지방이 asdf.
age = 29
b = f'asdf {age} asdf {name}'
print(b)
asdf 29 asdf 지방이
|
#1. .format()
1
2
3
|
b = "adf {} asdf.".format('지방이')
print(b)
adf 지방이 asdf.
|
1
2
3
4
|
a = 'aasdf {age} asdf asdf {name} adf.'.format(name = '지방이', age = 29)
print(a)
aasdf 29 asdf asdf 지방이 adf.
|
#2. %d, %s
문자열 포매팅
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#숫자형
numb = 3
b = "adf %d asdf." %numb
print(b)
adf 3 asdf.
#문자열
string = 'three'
b = "adf %s asdf." %string
print(b)
adf three asdf.
#두개 한번에
numb = 3
days = 'three'
b = "adf %d adsf %s asdf." %(numb, days)
print(b)
adf 3 adsf three asdf.
|