티스토리 뷰

Python

slicing, startswith

lluna 2021. 12. 6. 09:35

문자열 슬라이싱

문자열 startswith 메서드

a = 'abcdefg'

b = a[-1]      # last item in the array
c = a[-2:]     # last two items in the array
d = a[:-2]     # everything except the last two items
e = a[:-1]     # everything except the last one item
f = a[::-1]    # all items in the array, reversed
g = a[1::-1]   # the first two items, reversed
h = a[:-3:-1]  # the last two items, reversed
i = a[-3::-1]  # everything except the last two items, reversed
j = a.startswith('ab')

print(a)
print(b) # g
print(c) # fg
print(d) # abcde
print(e) # abcdef
print(f) # gfedcba
print(g) # ba
print(h) # gf
print(i) # edcba
print(j) # True
댓글