Phương thức chuỗi
Thí dụ
Thay thế từ "chuối":
txt = "Tôi thích chuối"
x = txt.replace ["chuối", "táo"]
print[x]
Hãy tự mình thử »
Định nghĩa và cách sử dụng
Phương thức
geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks3 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. All occurrences of the specified phrase will be replaced, if nothing else is specified.
Cú pháp
String.replace [OldValue, NewValue, Count]
Giá trị tham số
OldValue | Yêu cầu. Chuỗi để tìm kiếm |
NewValue | Yêu cầu. Chuỗi để thay thế giá trị cũ bằng |
đếm | Không bắt buộc. Một số chỉ định có bao nhiêu lần xuất hiện của giá trị cũ bạn muốn thay thế. Mặc định là tất cả các lần xuất hiện |
Nhiều ví dụ hơn
Thí dụ
Thay thế tất cả sự xuất hiện của từ "một":
TXT = "Một là một con ngựa đua, hai hai cũng là một."
x = txt.replace ["One", "ba"]
print[x]
Hãy tự mình thử »
Thí dụ
Định nghĩa và cách sử dụng
TXT = "Một là một con ngựa đua, hai hai cũng là một."
x = txt.replace ["One", "ba"]
print[x]
Hãy tự mình thử »
Phương thức chuỗi
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem 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.
Cú pháp thay thế []
Cú pháp: String.replace [cũ, mới, đếm] string.replace[old, new, count]
Parameters:
- Cũ - con cũ bạn muốn thay thế. old substring you want to replace.
- MỚI - Chất nền mới sẽ thay thế cho bộ nền cũ. new substring which would replace the old 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; [Optional ] the number of times you want to replace the old substring with the new substring.
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;It returns a copy of the string where all occurrences of a substring are replaced with another substring.
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.
Python3
geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks4
geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks5
geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks6
geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks7
geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks5
geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks9
gaaks for gaaks gaaks gaaks gaaks geas for geas geas geeks geeks0
gaaks for gaaks gaaks gaaks gaaks geas for geas geas geeks geeks1
gaaks for gaaks gaaks gaaks gaaks geas for geas geas geeks geeks2
gaaks for gaaks gaaks gaaks gaaks geas for geas geas geeks geeks3
gaaks for gaaks gaaks gaaks gaaks geas for geas geas geeks geeks4
gaaks for gaaks gaaks gaaks gaaks geas for geas geas geeks geeks5
gaaks for gaaks gaaks gaaks gaaks geas for geas geas geeks geeks4
gaaks for gaaks gaaks gaaks gaaks geas for geas geas geeks geeks7
Đầ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
geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks4
geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks5
string.replace[old_text, new_text, count]
0gaaks for gaaks gaaks gaaks gaaks geas for geas geas geeks geeks4
gaaks for gaaks gaaks gaaks gaaks geas for geas geas geeks geeks5
gaaks for gaaks gaaks gaaks gaaks geas for geas geas geeks geeks4
string.replace[old_text, new_text, count]
4string.replace[old_text, new_text, count]
5gaaks for gaaks gaaks gaaks gaaks geas for geas geas geeks geeks1
string.replace[old_text, new_text, count]
7string.replace[old_text, new_text, count]
8Đầu ra: & nbsp;
geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks
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ế [].count=3.
Python3
geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks4
geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks5
string.replace[old_text, new_text, count]
0gaaks for gaaks gaaks gaaks gaaks geas for geas geas geeks geeks4
string.replace[old_text, new_text, count]
4gaaks for gaaks gaaks gaaks gaaks geas for geas geas geeks geeks2
gaaks for gaaks gaaks gaaks gaaks geas for geas geas geeks geeks1
phrase = "I like to learn coding on the go"
# replace all instances of 'o' with 'a'
substituted_phrase = phrase.replace["o", "a" ]
print[phrase]
print[substituted_phrase]
#output
#I like to learn coding on the go
#I like ta learn cading an the ga
6string.replace[old_text, new_text, count]
8gaaks for gaaks gaaks gaaks gaaks geas for geas geas geeks geeks4
string.replace[old_text, new_text, count]
4phrase = "I like to learn coding on the go"
# replace only the first two instances of 'o' with 'a'
substituted_phrase = phrase.replace["o", "a", 2 ]
print[phrase]
print[substituted_phrase]
#output
#I like to learn coding on the go
#I like ta learn cading on the go
0gaaks for gaaks gaaks gaaks gaaks geas for geas geas geeks geeks1
phrase = "I like to learn coding on the go"
# replace all instances of 'o' with 'a'
substituted_phrase = phrase.replace["o", "a" ]
print[phrase]
print[substituted_phrase]
#output
#I like to learn coding on the go
#I like ta learn cading an the ga
6gaaks for gaaks gaaks gaaks gaaks geas for geas geas geeks geeks1
phrase = "I like to learn coding on the go"
# replace only the first two instances of 'o' with 'a'
substituted_phrase = phrase.replace["o", "a", 2 ]
print[phrase]
print[substituted_phrase]
#output
#I like to learn coding on the go
#I like ta learn cading on the go
4string.replace[old_text, new_text, count]
8Output:
gaaks for gaaks gaaks gaaks gaaks geas for geas geas geeks geeks
Trong bài viết này, bạn sẽ thấy cách sử dụng phương pháp
6 của Python để thực hiện sự thay thế phụ.phrase = "I like to learn coding on the go"
# replace only the first two instances of 'o' with 'a'
substituted_phrase = phrase.replace["o", "a", 2 ]
print[phrase]
print[substituted_phrase]
#output
#I like to learn coding on the go
#I like ta learn cading on the go
Bạn cũng sẽ thấy cách thực hiện thay thế chất nền không nhạy cảm trường hợp.
Bắt đầu nào!
Phương pháp python phrase = "I like to learn coding on the go"
# replace only the first two instances of 'o' with 'a'
substituted_phrase = phrase.replace["o", "a", 2 ]
print[phrase]
print[substituted_phrase]
#output
#I like to learn coding on the go
#I like ta learn cading on the go
6 làm gì?
phrase = "I like to learn coding on the go"
# replace only the first two instances of 'o' with 'a'
substituted_phrase = phrase.replace["o", "a", 2 ]
print[phrase]
print[substituted_phrase]
#output
#I like to learn coding on the go
#I like ta learn cading on the go
Khi sử dụng phương pháp Python
phrase = "I like to learn coding on the go"
# replace only the first two instances of 'o' with 'a'
substituted_phrase = phrase.replace["o", "a", 2 ]
print[phrase]
print[substituted_phrase]
#output
#I like to learn coding on the go
#I like ta learn cading on the go
6, bạn có thể thay thế mọi trường hợp của một ký tự cụ thể bằng một ký tự mới. Bạn thậm chí có thể thay thế toàn bộ chuỗi văn bản bằng một dòng văn bản mới mà bạn chỉ định.Phương thức
phrase = "I like to learn coding on the go"
# replace only the first two instances of 'o' with 'a'
substituted_phrase = phrase.replace["o", "a", 2 ]
print[phrase]
print[substituted_phrase]
#output
#I like to learn coding on the go
#I like ta learn cading on the go
6 trả về một bản sao của một chuỗi. Điều này có nghĩa là bộ nền cũ vẫn giữ nguyên, nhưng một bản sao mới được tạo ra - với tất cả các văn bản cũ đã được thay thế bằng văn bản mới.Phương pháp Python phrase = "I like to learn coding on the go"
# replace only the first two instances of 'o' with 'a'
substituted_phrase = phrase.replace["o", "a", 2 ]
print[phrase]
print[substituted_phrase]
#output
#I like to learn coding on the go
#I like ta learn cading on the go
6 hoạt động như thế nào? Một sự cố cú pháp
phrase = "I like to learn coding on the go"
# replace only the first two instances of 'o' with 'a'
substituted_phrase = phrase.replace["o", "a", 2 ]
print[phrase]
print[substituted_phrase]
#output
#I like to learn coding on the go
#I like ta learn cading on the go
Cú pháp cho phương thức
phrase = "I like to learn coding on the go"
# replace only the first two instances of 'o' with 'a'
substituted_phrase = phrase.replace["o", "a", 2 ]
print[phrase]
print[substituted_phrase]
#output
#I like to learn coding on the go
#I like ta learn cading on the go
6 trông như thế này:string.replace[old_text, new_text, count]
Hãy phá vỡ nó:
2 là tham số bắt buộc đầu tiên màphrase = "I like to learn coding on the go" # replace only the first instance of 'o' with 'a' substituted_phrase = phrase.replace["o", "a", 1 ] print[phrase] print[substituted_phrase] #output #I like to learn coding on the go #I like ta learn coding on the go
6 chấp nhận. Đó là nhân vật hoặc văn bản cũ mà bạn muốn thay thế. Đưa này trong dấu ngoặc kép.phrase = "I like to learn coding on the go" # replace only the first two instances of 'o' with 'a' substituted_phrase = phrase.replace["o", "a", 2 ] print[phrase] print[substituted_phrase] #output #I like to learn coding on the go #I like ta learn cading on the go
4 là tham số cần thiết thứ hai màphrase = "I like to learn coding on the go" # replace only the first instance of 'o' with 'a' substituted_phrase = phrase.replace["o", "a", 1 ] print[phrase] print[substituted_phrase] #output #I like to learn coding on the go #I like ta learn coding on the go
6 chấp nhận. Đó là ký tự hoặc văn bản mới mà bạn muốn thay thế ký tự/văn bản cũ bằng. Tham số này cũng cần được đặt trong các dấu ngoặc kép.phrase = "I like to learn coding on the go" # replace only the first two instances of 'o' with 'a' substituted_phrase = phrase.replace["o", "a", 2 ] print[phrase] print[substituted_phrase] #output #I like to learn coding on the go #I like ta learn cading on the go
6 là tham số thứ ba tùy chọn màphrase = "I like to learn coding on the go" # replace only the first instance of 'o' with 'a' substituted_phrase = phrase.replace["o", "a", 1 ] print[phrase] print[substituted_phrase] #output #I like to learn coding on the go #I like ta learn coding on the go
6 chấp nhận. Theo mặc định,phrase = "I like to learn coding on the go" # replace only the first two instances of 'o' with 'a' substituted_phrase = phrase.replace["o", "a", 2 ] print[phrase] print[substituted_phrase] #output #I like to learn coding on the go #I like ta learn cading on the go
6 sẽ thay thế tất cả các trường hợp của chuỗi con. Tuy nhiên, bạn có thể sử dụngphrase = "I like to learn coding on the go" # replace only the first two instances of 'o' with 'a' substituted_phrase = phrase.replace["o", "a", 2 ] print[phrase] print[substituted_phrase] #output #I like to learn coding on the go #I like ta learn cading on the go
6 để chỉ định số lần xuất hiện bạn muốn được thay thế.all instances of the substring. However, you can usephrase = "I like to learn coding on the go" # replace only the first instance of 'o' with 'a' substituted_phrase = phrase.replace["o", "a", 1 ] print[phrase] print[substituted_phrase] #output #I like to learn coding on the go #I like ta learn coding on the go
6 to specify the number of occurrences you want to be replaced.phrase = "I like to learn coding on the go" # replace only the first instance of 'o' with 'a' substituted_phrase = phrase.replace["o", "a", 1 ] print[phrase] print[substituted_phrase] #output #I like to learn coding on the go #I like ta learn coding on the go
Python phrase = "I like to learn coding on the go"
# replace only the first two instances of 'o' with 'a'
substituted_phrase = phrase.replace["o", "a", 2 ]
print[phrase]
print[substituted_phrase]
#output
#I like to learn coding on the go
#I like ta learn cading on the go
6 Ví dụ về mã phương thức
phrase = "I like to learn coding on the go"
# replace only the first two instances of 'o' with 'a'
substituted_phrase = phrase.replace["o", "a", 2 ]
print[phrase]
print[substituted_phrase]
#output
#I like to learn coding on the go
#I like ta learn cading on the go
Cách thay thế tất cả các phiên bản của một ký tự
Để thay đổi tất cả các trường hợp của một ký tự duy nhất, bạn sẽ thực hiện như sau:all instances of a single character, you would do the following:
phrase = "I like to learn coding on the go"
# replace all instances of 'o' with 'a'
substituted_phrase = phrase.replace["o", "a" ]
print[phrase]
print[substituted_phrase]
#output
#I like to learn coding on the go
#I like ta learn cading an the ga
Trong ví dụ trên, mỗi từ chứa ký tự
phrase = "The sun is strong today. I don't really like sun."
#replace all instances of the word 'sun' with 'wind'
substituted_phrase = phrase.replace["sun", "wind"]
print[phrase]
print[substituted_phrase]
#output
#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like wind.
1 được thay thế bằng ký tự phrase = "The sun is strong today. I don't really like sun."
#replace all instances of the word 'sun' with 'wind'
substituted_phrase = phrase.replace["sun", "wind"]
print[phrase]
print[substituted_phrase]
#output
#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like wind.
2.Trong ví dụ đó, có bốn trường hợp của nhân vật
phrase = "The sun is strong today. I don't really like sun."
#replace all instances of the word 'sun' with 'wind'
substituted_phrase = phrase.replace["sun", "wind"]
print[phrase]
print[substituted_phrase]
#output
#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like wind.
1. Cụ thể, nó đã được tìm thấy trong các từ phrase = "The sun is strong today. I don't really like sun."
#replace all instances of the word 'sun' with 'wind'
substituted_phrase = phrase.replace["sun", "wind"]
print[phrase]
print[substituted_phrase]
#output
#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like wind.
4, phrase = "The sun is strong today. I don't really like sun."
#replace all instances of the word 'sun' with 'wind'
substituted_phrase = phrase.replace["sun", "wind"]
print[phrase]
print[substituted_phrase]
#output
#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like wind.
5, phrase = "The sun is strong today. I don't really like sun."
#replace all instances of the word 'sun' with 'wind'
substituted_phrase = phrase.replace["sun", "wind"]
print[phrase]
print[substituted_phrase]
#output
#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like wind.
6 và phrase = "The sun is strong today. I don't really like sun."
#replace all instances of the word 'sun' with 'wind'
substituted_phrase = phrase.replace["sun", "wind"]
print[phrase]
print[substituted_phrase]
#output
#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like wind.
7.Điều gì sẽ xảy ra nếu bạn chỉ muốn thay đổi hai từ, như
phrase = "The sun is strong today. I don't really like sun."
#replace all instances of the word 'sun' with 'wind'
substituted_phrase = phrase.replace["sun", "wind"]
print[phrase]
print[substituted_phrase]
#output
#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like wind.
4 và phrase = "The sun is strong today. I don't really like sun."
#replace all instances of the word 'sun' with 'wind'
substituted_phrase = phrase.replace["sun", "wind"]
print[phrase]
print[substituted_phrase]
#output
#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like wind.
5, để chứa phrase = "The sun is strong today. I don't really like sun."
#replace all instances of the word 'sun' with 'wind'
substituted_phrase = phrase.replace["sun", "wind"]
print[phrase]
print[substituted_phrase]
#output
#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like wind.
2 thay vì phrase = "The sun is strong today. I don't really like sun."
#replace all instances of the word 'sun' with 'wind'
substituted_phrase = phrase.replace["sun", "wind"]
print[phrase]
print[substituted_phrase]
#output
#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like wind.
1?Cách chỉ thay thế một số trường hợp nhất định của một ký tự
Để chỉ thay đổi hai trường hợp của một ký tự, bạn sẽ sử dụng tham số
phrase = "I like to learn coding on the go"
# replace only the first instance of 'o' with 'a'
substituted_phrase = phrase.replace["o", "a", 1 ]
print[phrase]
print[substituted_phrase]
#output
#I like to learn coding on the go
#I like ta learn coding on the go
6 và đặt thành hai:phrase = "I like to learn coding on the go"
# replace only the first two instances of 'o' with 'a'
substituted_phrase = phrase.replace["o", "a", 2 ]
print[phrase]
print[substituted_phrase]
#output
#I like to learn coding on the go
#I like ta learn cading on the go
Nếu bạn chỉ muốn thay đổi phiên bản đầu tiên của một ký tự, bạn sẽ đặt tham số
phrase = "I like to learn coding on the go"
# replace only the first instance of 'o' with 'a'
substituted_phrase = phrase.replace["o", "a", 1 ]
print[phrase]
print[substituted_phrase]
#output
#I like to learn coding on the go
#I like ta learn coding on the go
6 thành một:phrase = "I like to learn coding on the go"
# replace only the first instance of 'o' with 'a'
substituted_phrase = phrase.replace["o", "a", 1 ]
print[phrase]
print[substituted_phrase]
#output
#I like to learn coding on the go
#I like ta learn coding on the go
Cách thay thế tất cả các phiên bản của chuỗi
Để thay đổi nhiều hơn một ký tự, quá trình trông giống nhau.
phrase = "The sun is strong today. I don't really like sun."
#replace all instances of the word 'sun' with 'wind'
substituted_phrase = phrase.replace["sun", "wind"]
print[phrase]
print[substituted_phrase]
#output
#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like wind.
Trong ví dụ trên, từ
phrase = "The sun is strong today. I don't really like sun."
#replace only the first instance of the word 'sun' with 'wind'
substituted_phrase = phrase.replace["sun", "wind", 1]
print[phrase]
print[substituted_phrase]
#output
#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like sun.
4 đã được thay thế bằng từ phrase = "The sun is strong today. I don't really like sun."
#replace only the first instance of the word 'sun' with 'wind'
substituted_phrase = phrase.replace["sun", "wind", 1]
print[phrase]
print[substituted_phrase]
#output
#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like sun.
5.Cách chỉ thay thế một số phiên bản nhất định của chuỗi
Nếu bạn chỉ muốn thay đổi phiên bản đầu tiên của
phrase = "The sun is strong today. I don't really like sun."
#replace only the first instance of the word 'sun' with 'wind'
substituted_phrase = phrase.replace["sun", "wind", 1]
print[phrase]
print[substituted_phrase]
#output
#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like sun.
4 thành phrase = "The sun is strong today. I don't really like sun."
#replace only the first instance of the word 'sun' with 'wind'
substituted_phrase = phrase.replace["sun", "wind", 1]
print[phrase]
print[substituted_phrase]
#output
#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like sun.
5, bạn sẽ sử dụng tham số phrase = "I like to learn coding on the go"
# replace only the first instance of 'o' with 'a'
substituted_phrase = phrase.replace["o", "a", 1 ]
print[phrase]
print[substituted_phrase]
#output
#I like to learn coding on the go
#I like ta learn coding on the go
6 và đặt nó thành một.phrase = "The sun is strong today. I don't really like sun."
#replace only the first instance of the word 'sun' with 'wind'
substituted_phrase = phrase.replace["sun", "wind", 1]
print[phrase]
print[substituted_phrase]
#output
#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like sun.
Cách thực hiện thay thế chuỗi con không nhạy cảm trường hợp trong Python
Hãy xem một ví dụ khác.
phrase = "I am learning Ruby. I really enjoy the ruby programming language!"
#replace the text "Ruby" with "Python"
substituted_text = phrase.replace["Ruby", "Python"]
print[substituted_text]
#output
#I am learning Python. I really enjoy the ruby programming language!
Trong trường hợp này, điều tôi thực sự muốn làm là thay thế tất cả các trường hợp của từ
phrase = "The sun is strong today. I don't really like sun."
#replace only the first instance of the word 'sun' with 'wind'
substituted_phrase = phrase.replace["sun", "wind", 1]
print[phrase]
print[substituted_phrase]
#output
#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like sun.
9 bằng
phrase = "I am learning Ruby. I really enjoy the ruby programming language!"
#replace the text "Ruby" with "Python"
substituted_text = phrase.replace["Ruby", "Python"]
print[substituted_text]
#output
#I am learning Python. I really enjoy the ruby programming language!
0.Tuy nhiên, có từ
phrase = "I am learning Ruby. I really enjoy the ruby programming language!"
#replace the text "Ruby" with "Python"
substituted_text = phrase.replace["Ruby", "Python"]
print[substituted_text]
#output
#I am learning Python. I really enjoy the ruby programming language!
1 với chữ thường
phrase = "I am learning Ruby. I really enjoy the ruby programming language!"
#replace the text "Ruby" with "Python"
substituted_text = phrase.replace["Ruby", "Python"]
print[substituted_text]
#output
#I am learning Python. I really enjoy the ruby programming language!
2, mà tôi cũng muốn thay đổi.Vì chữ cái đầu tiên bằng chữ thường, và không phải là chữ hoa như tôi đã chỉ định với
phrase = "The sun is strong today. I don't really like sun."
#replace only the first instance of the word 'sun' with 'wind'
substituted_phrase = phrase.replace["sun", "wind", 1]
print[phrase]
print[substituted_phrase]
#output
#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like sun.
9, nó vẫn giữ nguyên và không thay đổi thành
phrase = "I am learning Ruby. I really enjoy the ruby programming language!"
#replace the text "Ruby" with "Python"
substituted_text = phrase.replace["Ruby", "Python"]
print[substituted_text]
#output
#I am learning Python. I really enjoy the ruby programming language!
0.Phương pháp
phrase = "I like to learn coding on the go"
# replace only the first two instances of 'o' with 'a'
substituted_phrase = phrase.replace["o", "a", 2 ]
print[phrase]
print[substituted_phrase]
#output
#I like to learn coding on the go
#I like ta learn cading on the go
6 có tính nhạy cảm với trường hợp, và do đó nó thực hiện thay thế phụ nhạy cảm với trường hợp.Để thực hiện thay thế chuỗi con không nhạy cảm trường hợp, bạn sẽ phải làm một cái gì đó khác biệt.
Bạn sẽ cần sử dụng chức năng
phrase = "I am learning Ruby. I really enjoy the ruby programming language!"
#replace the text "Ruby" with "Python"
substituted_text = phrase.replace["Ruby", "Python"]
print[substituted_text]
#output
#I am learning Python. I really enjoy the ruby programming language!
6 và sử dụng cờ
phrase = "I am learning Ruby. I really enjoy the ruby programming language!"
#replace the text "Ruby" with "Python"
substituted_text = phrase.replace["Ruby", "Python"]
print[substituted_text]
#output
#I am learning Python. I really enjoy the ruby programming language!
7.Để sử dụng
phrase = "I am learning Ruby. I really enjoy the ruby programming language!"
#replace the text "Ruby" with "Python"
substituted_text = phrase.replace["Ruby", "Python"]
print[substituted_text]
#output
#I am learning Python. I really enjoy the ruby programming language!
6 bạn cần:- Sử dụng mô -đun
9, thông quaphrase = "I am learning Ruby. I really enjoy the ruby programming language!" #replace the text "Ruby" with "Python" substituted_text = phrase.replace["Ruby", "Python"] print[substituted_text] #output #I am learning Python. I really enjoy the ruby programming language!
geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks
00. - Speficy một biểu thức chính quy
geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks
01. - Đề cập đến những gì bạn muốn
geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks
02 mẫu. - Đề cập đến
geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks
03 bạn muốn thực hiện thao tác này trên. - Tùy chọn, chỉ định tham số
6 để làm cho việc thay thế chính xác hơn và chỉ định số lượng thay thế tối đa bạn muốn diễn ra.phrase = "I like to learn coding on the go" # replace only the first instance of 'o' with 'a' substituted_phrase = phrase.replace["o", "a", 1 ] print[phrase] print[substituted_phrase] #output #I like to learn coding on the go #I like ta learn coding on the go
- Cờ
7 cho biết biểu thức thông thường thực hiện một trận đấu không nhạy cảm với trường hợp.phrase = "I am learning Ruby. I really enjoy the ruby programming language!" #replace the text "Ruby" with "Python" substituted_text = phrase.replace["Ruby", "Python"] print[substituted_text] #output #I am learning Python. I really enjoy the ruby programming language!
Vì vậy, tất cả các cú pháp cùng nhau trông như thế này:
geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks0
Lấy ví dụ từ trước đó:
geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks1
Đây là cách tôi sẽ thay thế cả
phrase = "The sun is strong today. I don't really like sun."
#replace only the first instance of the word 'sun' with 'wind'
substituted_phrase = phrase.replace["sun", "wind", 1]
print[phrase]
print[substituted_phrase]
#output
#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like sun.
9 và
phrase = "I am learning Ruby. I really enjoy the ruby programming language!"
#replace the text "Ruby" with "Python"
substituted_text = phrase.replace["Ruby", "Python"]
print[substituted_text]
#output
#I am learning Python. I really enjoy the ruby programming language!
1 bằng
phrase = "I am learning Ruby. I really enjoy the ruby programming language!"
#replace the text "Ruby" with "Python"
substituted_text = phrase.replace["Ruby", "Python"]
print[substituted_text]
#output
#I am learning Python. I really enjoy the ruby programming language!
0:geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks2
Gói lên
Và ở đó bạn có nó - bây giờ bạn biết những điều cơ bản của sự thay thế chuỗi con. Hy vọng rằng bạn tìm thấy hướng dẫn này hữu ích.
Để tìm hiểu thêm về Python, hãy xem máy tính khoa học của Freecodecamp với chứng nhận Python.
Bạn sẽ bắt đầu từ những điều cơ bản và học hỏi một cách thân thiện với người mới bắt đầu. Bạn cũng sẽ xây dựng năm dự án vào cuối để đưa vào thực tế và giúp củng cố những gì bạn đã học.
Cảm ơn bạn đã đọc và mã hóa hạnh phúc!
Học mã miễn phí. Chương trình giảng dạy nguồn mở của Freecodecamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu