본문 바로가기
프로그래밍 언어/Python

[python] 문자열 변경

by nahkim 2023. 5. 7.

python의 경우 슬라이싱을 통해 문자열을 일무반 수정하는 것은 불가능하다.

그렇기 때문에 문자열을 수정하는 것이 아니라, 새로운 결과 변수를 선언하여 수정해야한다. 

str = "abcd"
str[1] = 'a'	# TypeError: 'str' object does not support item assignment

 

replace 함수를 사용할 경우

str = "abcd"

print(str.replace('b', 'a'))
# aacd