프로그래밍 언어/파이썬

문자열 슬라이싱

야식은진리다 2020. 6. 22. 19:58

문자열 인덱싱

파이썬도 다른 많은 언어들 처럼 문자열 인덱싱 기능을 지원한다.

a = "Life is too short, You need Python"

print(a[0]) #L
print(a[-1]) #n

 

문자열 슬라이싱

a = "Life is too short, You need Python"

print(a[0:5]) #Life
print(a[0:-17]) #Life is too short

print(a[4:]) # is too short, You need Python
print(a[:5]) #Life

주의 할 점은 a[0:5]의 범위는 0<=a<5와 같다라는것이다.

또 a[0:-5] 또한 마지막 번호는 포함하지 않고 0부터 -6까지의 문자열을 나타낸다.