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
str.replace[old, new [, count]]1 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.
Thí dụ
text = 'bat ball'
# replace b with c
replaced_text = text.replace['b', 'c']
print[replaced_text]
# Output: cat call
thay thế [] cú pháp
Đó là cú pháp là:
str.replace[old, new [, count]]
thay thế [] tham số
Phương thức
str.replace[old, new [, count]]1 có thể mất tối đa 3 tham số:
- Cựu Ước - Cựu Ước bạn muốn thay thế - old substring you want to replace
- Mới - Chất nền mới sẽ thay thế bộ nền cũ - new substring which will replace the old substring
- Đếm [Tùy chọn] - Số lần bạn muốn thay thế bộ nền cũ bằng cách [optional] - the number of times you want to replace the old substring with the new substring
Lưu ý: Nếu số lượng không được chỉ định, phương thức str.replace[old, new [, count]]
1 thay thế tất cả các lần xuất hiện của chuỗi con cũ bằng chuỗi con mới.: If count is not specified, the
str.replace[old, new [, count]]1 method replaces all occurrences of the old substring with the new substring.
thay thế [] giá trị trả về
Phương thức
str.replace[old, new [, count]]1 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
hurt, hurt heart Let it be, don't let it be, don't let it be, let it be
Thêm ví dụ về Chuỗi thay thế []
song = '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]]
Đầu ra
Original string: cold, cold heart Replaced string: celd, celd heart let it be, let it be, let it be
Phương thức python String thay thế [] Phương thức thay thế [] thay thế một cụm từ được chỉ định bằng một cụm từ được chỉ định khác. Lưu ý: Tất cả các lần xuất hiện của cụm từ được chỉ định sẽ được thay thế, nếu không có gì khác được chỉ định.
Làm thế nào để bạn thay thế một chữ số trong một nhân vật trong Python?
for x in L:
for y in x:
if y == 0:
x[x.index[y]] = 1
Khoa học dữ liệu thực tế sử dụng Python Xem xét dịch chuyển hoạt động [C, X], trong đó C là bất kỳ ký tự nào và X là một số [chữ số], điều này sẽ tìm thấy ký tự X sau c. Vì vậy, ví dụ, Shift ['P', 5] = 'U' và Shift ['A', 0] = 'A'.
for x in L:
if x == 0:
L[L.index[x]] = 1
Nhận đầu vào dưới dạng số nguyên từ người dùng ..
Sau đó chuyển đổi số nguyên thành chuỗi bằng phương thức str [] ..
Thay thế tất cả '0' bằng '5' bằng phương thức thay thế [] ..
Nhận đầu vào dưới dạng số nguyên từ người dùng ..
Sau đó chuyển đổi số nguyên thành chuỗi bằng phương thức str [] ..
Thay thế tất cả '0' bằng '5' bằng phương thức thay thế [] ..
Sau đó chuyển đổi chuỗi thành một số nguyên bằng phương thức int [] ..
Cuối cùng, in số nguyên được chuyển đổi làm đầu ra .. : test_str = ‘G4G is 4 all No. 1 Geeks’, K = ‘#’
Output : G#G is # all No. # Geeks
Explanation : All numbers replaced by K.Thay thế [] làm gì trong Python? : test_str = ‘G4G is 4 all No. Geeks’, K = ‘#’
Output : G#G is # all No. Geeks
Explanation : All numbers replaced by K.
Phương thức python String thay thế [] Phương thức thay thế [] thay thế một cụm từ được chỉ định bằng một cụm từ được chỉ định khác. Lưu ý: Tất cả các lần xuất hiện của cụm từ được chỉ định sẽ được thay thế, nếu không có gì khác được chỉ định.
Làm thế nào để bạn thay thế một chữ số trong một nhân vật trong Python?
Python3
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]]
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]]
2 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]]
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]]
6 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]]
7 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]]
8song = '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]]
9hurt, hurt heart Let it be, don't let it be, don't let it be, let it be0
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]]
2 hurt, hurt heart Let it be, don't let it be, don't let it be, let it be2
hurt, hurt heart Let it be, don't let it be, don't let it be, let it be3
hurt, hurt heart Let it be, don't let it be, don't let it be, let it be4
hurt, hurt heart Let it be, don't let it be, don't let it be, let it be5
hurt, hurt heart Let it be, don't let it be, don't let it be, let it be6
hurt, hurt heart Let it be, don't let it be, don't let it be, let it be7
hurt, hurt heart Let it be, don't let it be, don't let it be, let it be8
hurt, hurt heart Let it be, don't let it be, don't let it be, let it be9
song = '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]]
0song = '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]]
2 song = '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]]
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]]
5song = '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]]
6 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]]
7 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]]
8song = '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]]
9Đầu ra
The original string is : G4G is 4 all No. 1 Geeks The resultant string : G@G is @ all No. @ Geeks
Phương thức số 2: Sử dụng regex [] + sub []
Trong đó, regex thích hợp được sử dụng để xác định các chữ số và sub [] được sử dụng để thực hiện thay thế.
Python3
Original string: cold, cold heart Replaced string: celd, celd heart let it be, let it be, let it be0
Original string: cold, cold heart Replaced string: celd, celd heart let it be, let it be, let it be1
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]]
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]]
2 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]]
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]]
6 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]]
7 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]]
8song = '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]]
9hurt, hurt heart Let it be, don't let it be, don't let it be, let it be0
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]]
2 hurt, hurt heart Let it be, don't let it be, don't let it be, let it be2
for x in L:
for y in x:
if y == 0:
x[x.index[y]] = 1
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]]
2 for x in L:
for y in x:
if y == 0:
x[x.index[y]] = 1
6for x in L:
for y in x:
if y == 0:
x[x.index[y]] = 1
7for x in L:
for y in x:
if y == 0:
x[x.index[y]] = 1
8song = '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]]
5song = '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]]
6 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]]
7 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]]
8for x in L:
if x == 0:
L[L.index[x]] = 1
4Đầu ra
The original string is : G4G is 4 all No. 1 Geeks The resultant string : G@G is @ all No. @ Geeks
Phương thức số 2: Sử dụng regex [] + sub []
Python3
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]]
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]]
2 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]]
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]]
6 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]]
7 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]]
8song = '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]]
9hurt, hurt heart Let it be, don't let it be, don't let it be, let it be0
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]]
2 hurt, hurt heart Let it be, don't let it be, don't let it be, let it be2
The original string is : G4G is 4 all No. 1 Geeks The resultant string : G@G is @ all No. @ Geeks7
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]]
2The original string is : G4G is 4 all No. 1 Geeks The resultant string : G@G is @ all No. @ Geeks9
hurt, hurt heart Let it be, don't let it be, don't let it be, let it be3
hurt, hurt heart Let it be, don't let it be, don't let it be, let it be4
hurt, hurt heart Let it be, don't let it be, don't let it be, let it be5
hurt, hurt heart Let it be, don't let it be, don't let it be, let it be6
hurt, hurt heart Let it be, don't let it be, don't let it be, let it be7
hurt, hurt heart Let it be, don't let it be, don't let it be, let it be8
hurt, hurt heart Let it be, don't let it be, don't let it be, let it be9
song = '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]]
0song = '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]]
2 song = '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]]
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]]
5song = '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]]
6 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]]
7 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]]
8song = '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]]
9Đầu ra
The original string is : G4G is 4 all No. 1 Geeks The resultant string : G@G is @ all No. @ Geeks
Phương thức số 2: Sử dụng regex [] + sub []
Trong đó, regex thích hợp được sử dụng để xác định các chữ số và sub [] được sử dụng để thực hiện thay thế.O[n]
Auxiliary Space: O[n]