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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 큰따옴표로 양쪽 둘러싸기
>>> "Hello World"
 
# 작은따옴표로 양쪽 둘러싸기
>>> 'Python is fun'
 
# 큰따옴표 3개를 연속으로 써서 양쪽 둘러싸기
>>> """Life is too short, You need python"""
 
# 작은따옴표 3개를 연속으로 써서 양쪽 둘러싸기
>>> '''Life is too short, You need python'''
 
 
# 여러 줄인 문자열을 변수에 대입하고 싶을 때
Life is too short
You need python
 
>>> multiline = "Life is too short\nYou need python"
 
>>> multiline='''
... Life is too short
... You need python
... '''
 
>>> multiline="""
... Life is too short
... You need python
... """
 
# 문자열 더해서 연결하기(Concatenation)
>>> head = "Python"
>>> tail = " is fun!"
>>> head + tail
'Python is fun!'
 
#문자열 곱하기
>>> a = "python"
>>> a * 2
'pythonpython'
 
# 문자열 인덱싱
>>> a = "Life is too short, You need Python"
>>> a[3]
'e'
 
# 문자열 슬라이싱 
- a[시작 위치:끝 위치]를 지정하면 끝 위치에 해당하는 것은 포함되지 않는다.
>>> a = "Life is too short, You need Python"
>>> a[0:4]
'Life'
 
- a[시작 위치:끝 위치]에서 끝 위치 부분을 생략하면 시작 위치부터 그 문자열의 끝까지 뽑아낸다.
>>> a[19:]
'You need Python'
 
- a[시작:끝]에서 시작 위치를 생략하면 문자열의 처음부터 끝까지 뽑아낸다.
>>> a[:17]
'Life is too short'
 
 
 
cs


+ Recent posts