programing

python 3.x에서 string.replace()를 사용하는 방법

prostudy 2022. 3. 28. 21:42
반응형

python 3.x에서 string.replace()를 사용하는 방법

string.replace()python 3.x에서는 더 이상 사용되지 않는다.이것을 하는 새로운 방법은 무엇인가?

2.x와 같이 를 사용한다.

예:

>>> 'Hello world'.replace('world', 'Guido')
'Hello Guido'

replace()의 방법이다.<class 'str'>python3:

>>> 'hello, world'.replace(',', ':')
'hello: world'

python 3의 replace() 메서드는 다음과 같은 간단한 방법으로 사용된다.

a = "This is the island of istanbul"
print (a.replace("is" , "was" , 3))

#3 is the maximum replacement that can be done in the string#

>>> Thwas was the wasland of istanbul

# Last substring 'is' in istanbul is not replaced by was because maximum of 3 has already been reached

str.replace()str.replace() 체인으로 사용할 수 있다.너 같은 끈이 있다고 생각해.'Testing PRI/Sec (#434242332;PP:432:133423846,335)'그리고 당신은 모든 것을 교체하기를 원한다.'#',':',';','/'와 계약하다.'-'. 이런 식으로(정상적인 방법으로) 교체할 수 있다.

>>> string = 'Testing PRI/Sec (#434242332;PP:432:133423846,335)'
>>> string = string.replace('#', '-')
>>> string = string.replace(':', '-')
>>> string = string.replace(';', '-')
>>> string = string.replace('/', '-')
>>> string
'Testing PRI-Sec (-434242332-PP-432-133423846,335)'

또는 이 방법(str.cslapt의 체인)

>>> string = 'Testing PRI/Sec (#434242332;PP:432:133423846,335)'.replace('#', '-').replace(':', '-').replace(';', '-').replace('/', '-')
>>> string
'Testing PRI-Sec (-434242332-PP-432-133423846,335)'

다음을 시도해 보십시오.

mystring = "This Is A String"
print(mystring.replace("String","Text"))

공식 문서:str.replacePython 3

공식 문서: 파이톤 3's

str.properties(구[, new], new[, count]

모든 하위 문자열 old가 new로 대체된 문자열의 복사본을 반환하십시오.선택적 인수 카운트가 지정되면 첫 번째 카운트만 대체된다.

해당의VSCode의 구문 통지는 다음과 같다.

여기에 이미지 설명을 입력하십시오.

str.throp(자신: str, old, new, count) -> str.

두 가지 사용 방법str.replace

  • 방법 1: builtin str의 교체 사용 ->str.replace(strVariable, old, new[, count])
replacedStr1 = str.replace(originStr, "from", "to")
  • 방법 2: str 변수의 교체 사용 ->strVariable.replace(old, new[, count])
replacedStr2 = originStr.replace("from", "to")

풀 데모

코드:

originStr = "Hello world"

# Use case 1: use builtin str's replace -> str.replace(strVariable, old, new[, count])
replacedStr1 = str.replace(originStr, "world", "Crifan Li")
print("case 1: %s -> %s" % (originStr, replacedStr1))

# Use case 2: use str variable's replace -> strVariable.replace(old, new[, count])
replacedStr2 = originStr.replace("world", "Crifan Li")
print("case 2: %s -> %s" % (originStr, replacedStr2))

출력:

case 1: Hello world -> Hello Crifan Li
case 2: Hello world -> Hello Crifan Li

스크린샷:

여기에 이미지 설명을 입력하십시오.

나의 관련(중국어) 게시물: 【详解】pypy 3 3 3strstrstrstrstrstr.replace

FYI, 문자열 내부의 임의의 위치 고정 단어에 일부 문자를 추가할 때(예: 접미사 -ly를 추가하여 형용사를 부사로 변경) 가독성을 위해 줄 끝에 접미사를 넣을 수 있다.이 작업을 수행하려면 다음을 사용하십시오.split()안쪽에replace():

s="The dog is large small"
ss=s.replace(s.split()[3],s.split()[3]+'ly')
ss
'The dog is largely small'
ss = s.replace(s.split()[1], +s.split()[1] + 'gy')
# should have no plus after the comma --i.e.,
ss = s.replace(s.split()[1], s.split()[1] + 'gy')

Simple Replace: .replace(이전, new, count) .

text = "Apples taste Good."
print(text.replace('Apples', 'Bananas'))          # use .replace() on a variable
Bananas taste Good.          <---- Output

print("Have a Bad Day!".replace("Bad","Good"))    # Use .replace() on a string
Have a Good Day!             <----- Output

print("Mom is happy!".replace("Mom","Dad").replace("happy","angry"))  #Use many times
Dad is angry!                <----- Output

참조URL: https://stackoverflow.com/questions/9452108/how-to-use-string-replace-in-python-3-x

반응형