파이썬 문자열 replace() 메서드
예시
"바나나"라는 단어를 교체하십시오.
txt = "I like bananas"
x = txt.replace("bananas", "apples")
print(x)
정의 및 사용
이 replace()
메서드는 지정된 구문을 다른 지정된 구문으로 바꿉니다.
참고: 다른 항목이 지정되지 않은 경우 지정된 구의 모든 항목이 대체됩니다.
통사론
string.replace(oldvalue, newvalue, count)
매개변수 값
Parameter | Description |
---|---|
oldvalue | Required. The string to search for |
newvalue | Required. The string to replace the old value with |
count | Optional. A number specifying how many occurrences of the old value you want to replace. Default is all occurrences |
더 많은 예
예시
"one"이라는 단어의 모든 항목 바꾸기:
txt = "one one was a race horse, two two was one too."
x =
txt.replace("one", "three")
print(x)
예시
"one"이라는 단어가 처음 나오는 두 개를 바꿉니다.
txt = "one one was a race horse, two two was one too."
x =
txt.replace("one", "three", 2)
print(x)