Hướng dẫn can you replace words in python? - bạn có thể thay thế các từ trong python không?

Phương thức chuỗi

Show

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 GeeksforGeeks
3 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ố

Tham sốSự mô tả
OldValueYêu cầu. Chuỗi để tìm kiếm
NewValueYêu cầu. Chuỗi để thay thế giá trị cũ bằng
đếmKhô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

  • Đọc
  • Bàn luận
  • 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 GeeksforGeeks
    4
    geeks for geeks 
    geeks for geeks
    GeeksforGeeks for GeeksforGeeks 
    GeeksforGeeks for GeeksforGeeks
    5
    geeks for geeks 
    geeks for geeks
    GeeksforGeeks for GeeksforGeeks 
    GeeksforGeeks for GeeksforGeeks
    6

    geeks for geeks 
    geeks for geeks
    GeeksforGeeks for GeeksforGeeks 
    GeeksforGeeks for GeeksforGeeks
    7
    geeks for geeks 
    geeks for geeks
    GeeksforGeeks for GeeksforGeeks 
    GeeksforGeeks for GeeksforGeeks
    5
    geeks for geeks 
    geeks for geeks
    GeeksforGeeks for GeeksforGeeks 
    GeeksforGeeks for GeeksforGeeks
    9
    gaaks for gaaks gaaks gaaks gaaks
    geas for geas geas geeks geeks
    0
    gaaks for gaaks gaaks gaaks gaaks
    geas for geas geas geeks geeks
    1
    gaaks for gaaks gaaks gaaks gaaks
    geas for geas geas geeks geeks
    2
    gaaks for gaaks gaaks gaaks gaaks
    geas for geas geas geeks geeks
    3

    gaaks for gaaks gaaks gaaks gaaks
    geas for geas geas geeks geeks
    4
    gaaks for gaaks gaaks gaaks gaaks
    geas for geas geas geeks geeks
    5

    gaaks for gaaks gaaks gaaks gaaks
    geas for geas geas geeks geeks
    4
    gaaks for gaaks gaaks gaaks gaaks
    geas for geas geas geeks geeks
    7

    Đầ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 GeeksforGeeks
    4
    geeks for geeks 
    geeks for geeks
    GeeksforGeeks for GeeksforGeeks 
    GeeksforGeeks for GeeksforGeeks
    5
    string.replace(old_text, new_text, count)
    
    0

    gaaks for gaaks gaaks gaaks gaaks
    geas for geas geas geeks geeks
    4
    gaaks for gaaks gaaks gaaks gaaks
    geas for geas geas geeks geeks
    5

    gaaks for gaaks gaaks gaaks gaaks
    geas for geas geas geeks geeks
    4
    string.replace(old_text, new_text, count)
    
    4
    string.replace(old_text, new_text, count)
    
    5
    gaaks for gaaks gaaks gaaks gaaks
    geas for geas geas geeks geeks
    1
    string.replace(old_text, new_text, count)
    
    7
    string.replace(old_text, new_text, count)
    
    8

    Đầu ra: & nbsp; 

    geeks for geeks 
    geeks for geeks
    GeeksforGeeks for GeeksforGeeks 
    GeeksforGeeks for GeeksforGeeks

    Hướng dẫn can you replace words in python? - bạn có thể thay thế các từ trong python không?

    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 GeeksforGeeks
    4
    geeks for geeks 
    geeks for geeks
    GeeksforGeeks for GeeksforGeeks 
    GeeksforGeeks for GeeksforGeeks
    5
    string.replace(old_text, new_text, count)
    
    0

    gaaks for gaaks gaaks gaaks gaaks
    geas for geas geas geeks geeks
    4
    string.replace(old_text, new_text, count)
    
    4
    gaaks for gaaks gaaks gaaks gaaks
    geas for geas geas geeks geeks
    2
    gaaks for gaaks gaaks gaaks gaaks
    geas for geas geas geeks geeks
    1
    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
    
    6
    string.replace(old_text, new_text, count)
    
    8

    gaaks for gaaks gaaks gaaks gaaks
    geas for geas geas geeks geeks
    4
    string.replace(old_text, new_text, count)
    
    4
    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
    
    0
    gaaks for gaaks gaaks gaaks gaaks
    geas for geas geas geeks geeks
    1
    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
    
    6
    gaaks for gaaks gaaks gaaks gaaks
    geas for geas geas geeks geeks
    1
    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
    string.replace(old_text, new_text, count)
    
    8

    Output:    

    gaaks for gaaks gaaks gaaks gaaks
    geas for geas geas geeks geeks

    Hướng dẫn can you replace words in python? - bạn có thể thay thế các từ trong python không?

    Trong bài viết này, bạn sẽ thấy cách sử dụng 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ủa Python để thực hiện sự thay thế phụ.

    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ì?

    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

    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ó:

    • 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
      
      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 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ấ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 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
      
      4 là tham số cần thiết thứ hai mà
      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 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 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 là tham số thứ ba tùy chọn mà
      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 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ụng
      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ỉ định số lần xuất hiện bạn muốn được thay thế.all instances of the substring. However, you can use
      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 to specify the number of occurrences you want to be replaced.

    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

    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
      
      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!
      
      9, thông qua
      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ố
      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 để 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.
    • 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 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.

    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 GeeksforGeeks
    0

    Lấy ví dụ từ trước đó:

    geeks for geeks 
    geeks for geeks
    GeeksforGeeks for GeeksforGeeks 
    GeeksforGeeks for GeeksforGeeks
    1

    Đâ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 GeeksforGeeks
    2

    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

    Có chức năng thay thế trong Python không?

    Phương thức thay thế () là một chức năng tích hợp được cung cấp trong lập trình Python.Nó 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.Thay thế () trả về một chuỗi mới trong đó chất nền cũ được thay thế bằng chuỗi con mới.. It replaces all the occurrences of the old substring with the new substring. Replace() returns a new string in which old substring is replaced with the new substring.

    Làm thế nào để bạn thay thế hai từ trong Python?

    Thay thế các chuỗi trong python (thay thế, dịch, re ...
    Thay thế chuỗi con: Thay thế () Chỉ định số lượng thay thế tối đa: Đếm.....
    Thay thế nhiều ký tự khác nhau: dịch ().
    Thay thế bằng regex: re.sub (), re.sub () thay thế nhiều chuỗi con bằng cùng một chuỗi.....
    Thay thế theo vị trí: Cắt lát ..

    Bạn có thể thay thế các chữ cái trong một chuỗi python không?

    Phương thức python forth () được sử dụng để tìm và thay thế các ký tự trong một chuỗi.Nó yêu cầu một chuỗi con phải được thông qua như một đối số;Các chức năng tìm thấy và thay thế nó.Phương thức thay thế () thường được sử dụng trong làm sạch dữ liệu.. It requires a substring to be passed as an argument; the function finds and replaces it. The replace() method is commonly used in data cleaning.