Python/TIL

[TIL] 파이썬 문자열 (replace, split, rstrip)

kyra 2025. 7. 20. 18:28
문자열 슬라이싱 (거꾸로출력)
string = 'PYTHON'

print(string[::-1])

#[시작점:끝점:건너뛰기/거꾸로]

 

문자열 치환 (replace)
phone_numb = "010-1111-2222"
print(phone_numb.replace("-", " "))

# replace를 사용해 문자열 치환​

 

string = 'abcdfe2a354a32a'

print(string.replace('a', 'A')) # Abcdfe2A354A32A 출력

 

문자열 분리 (split)
url = "http://sharebook.kr"
url_split = url.split(".")

print(url_split[-1]) # kr 출력

url_split # ['http://sharebook', 'kr'] 출력

 

 

오른쪽 공백 제거 (rstrip)
data = "039490     "
data = data.rstrip()

print(data) # 039490