Hướng dẫn how do you replace a character in a string in python 3? - làm cách nào để thay thế một ký tự trong chuỗi trong python 3?



Sự mô tả

Phương thức thay thế () trả về một bản sao của chuỗi trong đó các lần xuất hiện của cũ đã được thay thế bằng mới, tùy chọn hạn chế số lượng thay thế thành tối đa.replace() method returns a copy of the string in which the occurrences of old have been replaced with new, optionally restricting the number of replacements to max.

Cú pháp

Sau đây là Cú pháp cho phương thức thay thế () -replace() method −

str.replace(old, new[, max])

Thông số

  • Cũ - đây là cơ sở cũ được thay thế. − This is old substring to be replaced.

  • MỚI - đây là bộ nền mới, sẽ thay thế chuỗi con cũ. − This is new substring, which would replace old substring.

  • Tối đa - nếu đối số tùy chọn này được đưa ra tối đa, chỉ có lần xuất hiện đếm đầu tiên được thay thế. − If this optional argument max is given, only the first count occurrences are replaced.

Giá trị trả lại

Phương thức này trả về một bản sao của chuỗi với tất cả các lần xuất hiện của nền tảng cũ được thay thế bằng mới. Nếu đối số tùy chọn tối đa được đưa ra, chỉ thay thế số lần đếm đầu tiên được thay thế.

Thí dụ

Ví dụ sau đây cho thấy cách sử dụng phương thức thay thế ().

#!/usr/bin/python3

str = "this is string example....wow!!! this is really string"
print (str.replace("is", "was"))
print (str.replace("is", "was", 3))

Kết quả

Khi chúng tôi chạy trên chương trình, nó tạo ra kết quả sau -

thwas was string example....wow!!! thwas was really string
thwas was string example....wow!!! thwas is really string

Python_strings.htm

Thêm ví dụ về Chuỗi thay thế ()

Tài liệu chính thức cho

#!/usr/bin/python3

str = "this is string example....wow!!! this is really string"
print (str.replace("is", "was"))
print (str.replace("is", "was", 3))
3 của
#!/usr/bin/python3

str = "this is string example....wow!!! this is really string"
print (str.replace("is", "was"))
print (str.replace("is", "was", 3))
4

Tài liệu chính thức: Python 3

#!/usr/bin/python3

str = "this is string example....wow!!! this is really string"
print (str.replace("is", "was"))
print (str.replace("is", "was", 3))
3

str.replace (cũ, mới [, đếm])

Trả về một bản sao của chuỗi với tất cả các lần xuất hiện của phần phụ cũ được thay thế bằng mới. Nếu số lượng đối số tùy chọn được đưa ra, chỉ có các lần xuất hiện đầu tiên được thay thế.

Hướng dẫn how do you replace a character in a string in python 3? - làm cách nào để thay thế một ký tự trong chuỗi trong python 3?

Thông báo cú pháp tương ứng của ____ 16 là:

str.replace (tự: str, cũ, mới, đếm) -> str

  • Hai phương pháp để sử dụng
    #!/usr/bin/python3
    
    str = "this is string example....wow!!! this is really string"
    print (str.replace("is", "was"))
    print (str.replace("is", "was", 3))
    
    3
replacedStr1 = str.replace(originStr, "from", "to")
  • Phương pháp 2: Sử dụng thay thế của Biến STR ->
    #!/usr/bin/python3
    
    str = "this is string example....wow!!! this is really string"
    print (str.replace("is", "was"))
    print (str.replace("is", "was", 3))
    
    9
replacedStr2 = originStr.replace("from", "to")

Bản demo đầy đủ

code:

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))

output:

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

Ảnh chụp màn hình:

Hướng dẫn how do you replace a character in a string in python 3? - làm cách nào để thay thế một ký tự trong chuỗi trong python 3?

Bài đăng liên quan (Trung Quốc) của tôi: 【】 Python 3 中字 的 替换 替换 str.replace

Thêm ví dụ về Chuỗi thay thế ()

Trong hướng dẫn này, chúng tôi sẽ tìm hiểu về phương thức python thay thế () với sự trợ giúp của các ví dụ.

Phương thức thwas was string example....wow!!! thwas was really string thwas was string example....wow!!! thwas is really string 0 thay thế cho mỗi lần xuất hiện của ký tự/văn bản cũ trong chuỗi với ký tự/văn bản mới.

text = 'bat ball'

# replace b with c replaced_text = text.replace('b', 'c')

print(replaced_text) # Output: cat call


Thí dụ

thay thế () cú pháp

str.replace(old, new [, count]) 

Đó là cú pháp là:

thay thế () tham số

  • Phương thức
    thwas was string example....wow!!! thwas was really string
    thwas was string example....wow!!! thwas is really string
    
    0 có thể mất tối đa 3 tham số:
    - old substring you want to replace
  • Cựu Ước - Cựu Ước bạn muốn thay thế - new substring which will replace the old substring
  • Mới - Chất nền mới sẽ thay thế bộ nền cũ (optional) - the number of times you want to replace the old substring with the new substring

Đếm (Tùy chọn) - Số lần bạn muốn thay thế bộ nền cũ bằng cách: If count is not specified, the

thwas was string example....wow!!! thwas was really string
thwas was string example....wow!!! thwas is really string
0 method replaces all occurrences of the old substring with the new substring.


thay thế () giá trị trả về

Phương thức

thwas was string example....wow!!! thwas was really string
thwas was string example....wow!!! thwas is really string
0 trả về một bản sao của chuỗi trong đó chuỗi con cũ được thay thế bằng chuỗi con mới.Chuỗi ban đầu không thay đổi.

Nếu không tìm thấy chuỗi con cũ, nó sẽ trả về bản sao của chuỗi gốc.


Ví dụ 1: Sử dụng thay thế ()

song = 'cold, cold heart'

# replacing 'cold' with 'hurt' print(song.replace('cold', 'hurt'))

song = 'Let it be, let it be, let it be, let it be'

# replacing only two occurences of 'let' print(song.replace('let', "don't let", 2))

Đầu ra

#!/usr/bin/python3

str = "this is string example....wow!!! this is really string"
print (str.replace("is", "was"))
print (str.replace("is", "was", 3))
0

Thêm ví dụ về Chuỗi thay thế ()

#!/usr/bin/python3

str = "this is string example....wow!!! this is really string"
print (str.replace("is", "was"))
print (str.replace("is", "was", 3))
1

Đầu ra

#!/usr/bin/python3

str = "this is string example....wow!!! this is really string"
print (str.replace("is", "was"))
print (str.replace("is", "was", 3))
2