Hướng dẫn can strings be replaced in python? - chuỗi có thể được thay thế trong python không?

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

Show

Phương thức replace() 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 replace() 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 replace() 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 replace() method replaces all occurrences of the old substring with the new substring.


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

Phương thức replace() 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 thay thế () trả về một chuỗi mới với một, một số hoặc tất cả các kết quả của một mẫu được thay thế bằng cách thay thế. Mẫu có thể là một chuỗi hoặc một regexp và thay thế có thể là một chuỗi hoặc một hàm được gọi cho mỗi trận đấu. Nếu mẫu là một chuỗi, chỉ có sự xuất hiện đầu tiên sẽ được thay thế.


Bạn có thể thay thế nhiều chuỗi trong Python không?

Python cung cấp phương thức thay thế () để xử lý các ký tự thay thế (đơn hoặc nhiều) trong một chuỗi. Phương thức thay thế trả về một đối tượng mới (chuỗi) thay thế các trường (ký tự) được chỉ định bằng các giá trị mới.

Phương thức chuỗi

Thí dụ

print(x)

Thay thế từ "chuối":


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

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

Hãy tự mình thử » All occurrences of the specified phrase will be replaced, if nothing else is specified.


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

Phương thức replace() 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.

Cú phápString.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

Bạn có thể thay thế nhiều chuỗi trong Python không?

Python cung cấp phương thức thay thế () để xử lý các ký tự thay thế (đơn hoặc nhiều) trong một chuỗi. Phương thức thay thế trả về một đối tượng mới (chuỗi) thay thế các trường (ký tự) được chỉ định bằng các giá trị mới.

Phương thức chuỗi

Thí dụ

print(x)

Thay thế từ "chuối":

Bạn có thể thay thế nhiều chuỗi trong Python không?

Python cung cấp phương thức thay thế () để xử lý các ký tự thay thế (đơn hoặc nhiều) trong một chuỗi. Phương thức thay thế trả về một đối tượng mới (chuỗi) thay thế các trường (ký tự) được chỉ định bằng các giá trị mới.

Phương thức chuỗi

Thí dụ

print(x)

Thay thế từ "chuối":


Phương thức thay thế () trả về một chuỗi mới với một, một số hoặc tất cả các kết quả của một mẫu được thay thế bằng cách thay thế. Mẫu có thể là một chuỗi hoặc một regexp và thay thế có thể là một chuỗi hoặc một hàm được gọi cho mỗi trận đấu. Nếu mẫu là một chuỗi, chỉ có sự xuất hiện đầu tiên sẽ được thay thế.


Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọcreplace() in Python returns a copy of the string where all occurrences of a substring are replaced with another substring. 

    Bàn luận

    Việc thay thế () trong Python trả về một bản sao của chuỗi trong đó tất cả các lần xuất hiện của một chuỗi con được thay thế bằng một chuỗi con khác. & NBSP; string.replace(old, new, count)

    Parameters: 

    • Cú pháp thay thế () old substring you want to replace.
    • Cú pháp: String.replace (cũ, mới, đếm) new substring which would replace the old substring.
    • Cũ - con cũ bạn muốn thay thế. (Optional ) the number of times you want to replace the old substring with the new substring. 

    MỚI - Chất nền mới sẽ thay thế cho bộ nền cũ.It returns a copy of the string where all occurrences of a substring are replaced with another substring. 

    Đếm - (Tùy chọn) Số lần bạn muốn thay thế bộ con cũ bằng bộ nền mới. & NBSP;

    Giá trị trả về: Nó trả về một bản sao của chuỗi trong đó tất cả các lần xuất hiện của một chuỗi con được thay thế bằng một chuỗi con khác. & NBSP;

    Python3

    Thay thế tất cả các phiên bản của một ký tự bằng cách sử dụng thay thế ()

    Trong ví dụ này, chúng tôi chỉ thay thế một ký tự duy nhất từ ​​một chuỗi đã cho. Phương thức thay thế python () là nhạy cảm trường hợp, và do đó nó thực hiện thay thế chuỗi con nhạy cảm trường hợp, tức là r trong không thay đổi.

    song = 'cold, cold heart'
    
    

    # replacing 'cold' with 'hurt' print(song.replace('cold', 'hurt'))

    song = 'Let it be, let it be, let it be, let it be'

    # replacing only two occurences of 'let' print(song.replace('let', "don't let", 2))

    4
    song = 'cold, cold heart'
    
    

    # replacing 'cold' with 'hurt' print(song.replace('cold', 'hurt'))

    song = 'Let it be, let it be, let it be, let it be'

    # replacing only two occurences of 'let' print(song.replace('let', "don't let", 2))

    5

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

    4
    song = 'cold, cold heart'
    
    

    # replacing 'cold' with 'hurt' print(song.replace('cold', 'hurt'))

    song = 'Let it be, let it be, let it be, let it be'

    # replacing only two occurences of 'let' print(song.replace('let', "don't let", 2))

    7

    str.replace(old, new [, count]) 
    4
    str.replace(old, new [, count]) 
    5
    str.replace(old, new [, count]) 
    6
     

    grrks FOR grrks
    geeks FOR geeks

    str.replace(old, new [, count]) 7str.replace(old, new [, count]) 5 str.replace(old, new [, count]) 9song = 'cold, cold heart' # replacing 'cold' with 'hurt' print(song.replace('cold', 'hurt')) song = 'Let it be, let it be, let it be, let it be' # replacing only two occurences of 'let' print(song.replace('let', "don't let", 2))0song = 'cold, cold heart' # 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))3

    Đầu ra: & nbsp;

    Python3

    Thay thế tất cả các phiên bản của chuỗi

    song = 'cold, cold heart'
    
    

    # replacing 'cold' with 'hurt' print(song.replace('cold', 'hurt'))

    song = 'Let it be, let it be, let it be, let it be'

    # replacing only two occurences of 'let' print(song.replace('let', "don't let", 2))

    4
    song = 'cold, cold heart'
    
    

    # replacing 'cold' with 'hurt' print(song.replace('cold', 'hurt'))

    song = 'Let it be, let it be, let it be, let it be'

    # replacing only two occurences of 'let' print(song.replace('let', "don't let", 2))

    5

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

    4
    hurt, hurt heart
    Let it be, don't let it be, don't let it be, let it be
    4
    hurt, hurt heart
    Let it be, don't let it be, don't let it be, let it be
    5
    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
    hurt, hurt heart
    Let it be, don't let it be, don't let it be, let it be
    7
    hurt, hurt heart
    Let it be, don't let it be, don't let it be, let it be
    8

    str.replace(old, new [, count]) 
    4
    str.replace(old, new [, count]) 
    5
    str.replace(old, new [, count]) 
    6
     

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

    Hướng dẫn can strings be replaced in python? - chuỗi có thể được thay thế trong python không?

    str.replace(old, new [, count]) 7str.replace(old, new [, count]) 5 str.replace(old, new [, count]) 9song = 'cold, cold heart' # replacing 'cold' with 'hurt' print(song.replace('cold', 'hurt')) song = 'Let it be, let it be, let it be, let it be' # replacing only two occurences of 'let' print(song.replace('let', "don't let", 2))0song = 'cold, cold heart' # 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))3

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

    Python3

    Thay thế tất cả các phiên bản của chuỗi

    song = 'cold, cold heart'
    
    

    # replacing 'cold' with 'hurt' print(song.replace('cold', 'hurt'))

    song = 'Let it be, let it be, let it be, let it be'

    # replacing only two occurences of 'let' print(song.replace('let', "don't let", 2))

    4
    hurt, hurt heart
    Let it be, don't let it be, don't let it be, let it be
    4
    song = 'cold, cold heart'
    
    

    # replacing 'cold' with 'hurt' print(song.replace('cold', 'hurt'))

    song = 'Let it be, let it be, let it be, let it be'

    # replacing only two occurences of 'let' print(song.replace('let', "don't let", 2))

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

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

    6
    hurt, hurt heart
    Let it be, don't let it be, don't let it be, let it be
    8

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

    4
    hurt, hurt heart
    Let it be, don't let it be, don't let it be, let it be
    4
    Original string: cold, cold heart
    Replaced string: celd, celd heart
    let it be, let it be, let it be
    0
    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
    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))

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

    1
    Original string: cold, cold heart
    Replaced string: celd, celd heart
    let it be, let it be, let it be
    4
    hurt, hurt heart
    Let it be, don't let it be, don't let it be, let it be
    8

    Output:    

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

    Bạn có thể sử dụng thay thế trên một chuỗi không?

    Phương thức thay thế () trả về một chuỗi mới với một, một số hoặc tất cả các kết quả của một mẫu được thay thế bằng cách thay thế.Mẫu có thể là một chuỗi hoặc một regexp và thay thế có thể là một chuỗi hoặc một hàm được gọi cho mỗi trận đấu.Nếu mẫu là một chuỗi, chỉ có sự xuất hiện đầu tiên sẽ được thay thế. . The pattern can be a string or a RegExp , and the replacement can be a string or a function called for each match. If pattern is a string, only the first occurrence will be replaced.

    Bạn có thể thay thế nhiều chuỗi trong Python không?

    Python cung cấp phương thức thay thế () để xử lý các ký tự thay thế (đơn hoặc nhiều) trong một chuỗi.Phương thức thay thế trả về một đối tượng mới (chuỗi) thay thế các trường (ký tự) được chỉ định bằng các giá trị mới.. The replace method returns a new object (string) replacing specified fields (characters) with new values.