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ụ.
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.
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
ĐọcBàn luậnXem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọcreplace() in Python returns a copy of
the string where all occurrences of a substring are replaced with another substring.
Bàn luận
Việc thay thế () trong Python trả về một bản sao của chuỗi trong đó tất cả các lần xuất hiện của một chuỗi con được thay thế bằng một chuỗi con khác. & NBSP; string.replace(old, new, count)
Parameters:
- Cú pháp thay thế () old substring you want to replace.
- Cú pháp: String.replace (cũ, mới, đếm) new substring which would replace the old substring.
- Cũ - con cũ bạn muốn thay thế. (Optional ) the number of times you want to
replace the old substring with the new substring.
MỚI - Chất nền mới sẽ thay thế cho bộ nền cũ.It returns a copy of the string where all occurrences of a substring are replaced with another substring.
Đếm - (Tùy chọn) Số lần bạn muốn thay thế bộ con cũ bằng bộ nền mới. & NBSP;
Giá trị trả về: Nó trả về một bản sao của chuỗi trong đó tất cả các lần xuất hiện của một chuỗi con được thay thế bằng một chuỗi con khác. & NBSP;
Python3
Thay thế tất cả các phiên bản của một ký tự bằng cách sử dụng thay thế ()
Trong ví dụ này, chúng tôi chỉ thay thế một ký tự duy nhất từ một chuỗi đã cho. Phương thức thay thế python () là nhạy cảm trường hợp, và do đó nó thực hiện thay thế chuỗi con nhạy cảm trường hợp, tức là r trong không thay đổi.
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))
3song = '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))
4song = '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))
3song = '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))
6Đầu ra: & nbsp;
grrks FOR grrks
geeks FOR geeks
Thay thế tất cả các phiên bản của chuỗi
Ở đây, chúng tôi đã thay thế tất cả các chuyên viên máy tính bằng geekSforGeek bằng hàm thay thế ().
Python3
str.replace(old, new [, count])
3str.replace(old, new [, count])
4 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))
9song = '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))
3song = '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))
4song = '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))
3hurt, hurt heart
Let it be, don't let it be, don't let it be, let it be
3hurt, hurt heart
Let it be, don't let it be, don't let it be, let it be
4song = '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))
0hurt, hurt heart
Let it be, don't let it be, don't let it be, let it be
6hurt, hurt heart
Let it be, don't let it be, don't let it be, let it be
7Đầu ra: & nbsp;
geeks for geeks
geeks for geeks
GeeksforGeeks for GeeksforGeeks
GeeksforGeeks for GeeksforGeeks
Chỉ thay thế một số trường hợp nhất định bằng cách sử dụng thay thế ()
Trong ví dụ này, chúng tôi đang thay thế một số từ nhất định. tức là, EK EK, với một người khác với số lượng = 3.count=3.
Python3
str.replace(old, new [, count])
3str.replace(old, new [, count])
4 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))
9song = '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))
3hurt, hurt heart
Let it be, don't let it be, don't let it be, let it be
3song = '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))
1song = '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))
0song = 'cold, cold heart'
replaced_song = song.replace('o', 'e')
# The original string is unchanged
print('Original string:', song)
print('Replaced string:', replaced_song)
song = 'let it be, let it be, let it be'
# maximum of 0 substring is replaced
# returns copy of the original string
print(song.replace('let', 'so', 0))
5hurt, hurt heart
Let it be, don't let it be, don't let it be, let it be
7song = '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))
3hurt, hurt heart
Let it be, don't let it be, don't let it be, let it be
3song = 'cold, cold heart'
replaced_song = song.replace('o', 'e')
# The original string is unchanged
print('Original string:', song)
print('Replaced string:', replaced_song)
song = 'let it be, let it be, let it be'
# maximum of 0 substring is replaced
# returns copy of the original string
print(song.replace('let', 'so', 0))
9song = '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))
0song = 'cold, cold heart'
replaced_song = song.replace('o', 'e')
# The original string is unchanged
print('Original string:', song)
print('Replaced string:', replaced_song)
song = 'let it be, let it be, let it be'
# maximum of 0 substring is replaced
# returns copy of the original string
print(song.replace('let', 'so', 0))
5song = '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))
0Original string: cold, cold heart
Replaced string: celd, celd heart
let it be, let it be, let it be
3hurt, hurt heart
Let it be, don't let it be, don't let it be, let it be
7Output:
gaaks for gaaks gaaks gaaks gaaks
geas for geas geas geeks geeks
Phương thức Chuỗi thay thế () sẽ thay thế một ký tự hoặc chuỗi con bằng ký tự hoặc chuỗi khác.Cú pháp cho phương thức thay thế () là String_Name.Repace (Old_String, New_String) với Old_String là nền tảng mà bạn muốn thay thế và New_String là chất nền sẽ diễn ra.string_name. replace(old_string, new_string) with old_string being the substring you'd like to replace and new_string being the substring that will take its place.