Hướng dẫn how to replace a substring in python - cách thay thế một chuỗi con trong python

Xem thảo luận

Show

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. 

    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.

    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

    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
    2

    string ____10

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

    grrks FOR grrks
    geeks FOR geeks

    geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks2geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks0 geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks4geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks5geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks6geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks7 geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks8

    Đầu ra: & nbsp;

    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ế ().

    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

    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
    9
    text = 'bat ball'
    
    

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

    print(replaced_text) # Output: cat call
    0
    geeks for geeks 
    geeks for geeks
    GeeksforGeeks for GeeksforGeeks 
    GeeksforGeeks for GeeksforGeeks
    6
    text = 'bat ball'
    
    

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

    print(replaced_text) # Output: cat call
    2
    text = 'bat ball'
    
    

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

    print(replaced_text) # Output: cat call
    3

    string ____10

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

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

    Hướng dẫn how to replace a substring in python - cách thay thế một chuỗi con trong python

    geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks2geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks0 geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks4geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks5geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks6geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks7 geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks8

    Đầu ra: & nbsp;count=3.

    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ế ().

    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
    9
    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
    6
    str.replace(old, new [, count]) 
    1
    text = 'bat ball'
    
    

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

    print(replaced_text) # Output: cat call
    3

    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
    9
    str.replace(old, new [, count]) 
    5
    geeks for geeks 
    geeks for geeks
    GeeksforGeeks for GeeksforGeeks 
    GeeksforGeeks for GeeksforGeeks
    6
    str.replace(old, new [, count]) 
    1
    geeks for geeks 
    geeks for geeks
    GeeksforGeeks for GeeksforGeeks 
    GeeksforGeeks for GeeksforGeeks
    6
    str.replace(old, new [, count]) 
    9
    text = 'bat ball'
    
    

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

    print(replaced_text) # Output: cat call
    3

    Output:    

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

    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.


    Thí dụ

    Phương thức chuỗi

    Thay thế từ "chuối":

    txt = "Tôi thích chuối"

    print(x)

    x = txt.replace ("chuối", "táo")


    Hãy tự mình thử »

    Định nghĩa và cách sử dụng

    Phương thức

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

    1 thay thế một cụm từ được chỉ định bằng một cụm từ được chỉ định khác. All occurrences of the specified phrase will be replaced, if nothing else is specified.


    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.

    Cú pháp

    String.replace (OldValue, NewValue, Count)

    Giá trị tham sốTham số
    Sự mô tảOldValue
    Yêu cầu. Chuỗi để tìm kiếmNewValue
    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

    Thí dụ

    Nhiều ví dụ hơn

    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."

    print(x)

    x = txt.replace ("chuối", "táo")

    Thí dụ

    Hãy tự mình thử »

    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."

    print(x)

    x = txt.replace ("chuối", "táo")


    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.


    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

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

    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

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

    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

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

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

    1 method replaces all occurrences of the old substring with the new substring.


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

    Phương thức

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

    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

    Làm thế nào để bạn thay thế một chuỗi con trong Python?

    Cú pháp thay thế ()..
    Cú pháp: String.replace (cũ, mới, đếm).
    Parameters:.
    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 ..

    Làm cách nào để thay thế một chuỗi con bằng một chuỗi khác?

    Thuật toán để thay thế một chuỗi con trong một chuỗi nhập chuỗi đầy đủ (S1). Nhập chuỗi con từ chuỗi đầy đủ (S2). Nhập chuỗi được thay thế bằng chuỗi con (S3). Tìm chuỗi con từ chuỗi đầy đủ và thay thế chuỗi con mới bằng chuỗi con cũ (tìm S2 từ S1 và thay thế S1 bằng S3).Input the full string (s1). Input the substring from the full string (s2). Input the string to be replaced with the substring (s3). Find the substring from the full string and replace the new substring with the old substring (Find s2 from s1 and replace s1 by s3).

    Chúng ta có thể thay thế các chuỗi trong Python không?

    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.The replace() method replaces a specified phrase with another specified phrase. Note: All occurrences of the specified phrase will be replaced, if nothing else is specified.

    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.