Hướng dẫn how do you change part of a string in python? - làm thế nào để bạn thay đổi một phần của một chuỗi trong python?

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

Cú pháp: String.replace (SearchValue, NewValue) ....

Ví dụ 1:.

  • Đầu ra: Chuỗi ban đầu là GeekSforGeek Chuỗi được thay thế là GeekSandGeek ..
  • Cải thiện bài viết
  • Cú pháp: String.replace (SearchValue, NewValue) ....

    Ví dụ 1:.

    Đầu ra: Chuỗi ban đầu là GeekSforGeek Chuỗi được thay thế là GeekSandGeek ..replace() 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

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

    str.replace(old, new [, count]) 
    6
    str.replace(old, new [, count]) 
    4
    str.replace(old, new [, count]) 
    8
    str.replace(old, new [, count]) 
    9
    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))

    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'
    
    

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

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

    6

    Đầ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

    str.replace(old, new [, count]) 
    3
    str.replace(old, new [, count]) 
    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))

    9

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

    3
    hurt, hurt heart
    Let it be, don't let it be, don't let it be, let it be
    3
    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))

    0
    hurt, hurt heart
    Let it be, don't let it be, don't let it be, let it be
    6
    hurt, hurt heart
    Let it be, don't let it be, don't let it be, let it be
    7

    Đầu ra: & nbsp; 

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

    Hướng dẫn how do you change part of a string in python? - làm thế nào để bạn thay đổi một phần của một chuỗi trong python?

    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

    str.replace(old, new [, count]) 
    3
    str.replace(old, new [, count]) 
    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))

    9

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

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

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

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

    3
    hurt, hurt heart
    Let it be, don't let it be, don't let it be, let it be
    3
    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))

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

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

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

    0
    Original string: cold, cold heart
    Replaced string: celd, celd heart
    let it be, let it be, let it be
    3
    hurt, hurt heart
    Let it be, don't let it be, don't let it be, let it be
    7

    Output:    

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

    Làm thế nào để bạn thay thế một phần cụ thể của chuỗi?

    Một trong những phương pháp đơn giản và đơn giản nhất để thay thế một chuỗi con là sử dụng thay thế, thay thế hoặc thay thế trước một lớp chuỗi.using the replace, replaceAll or replaceFirst of a String class.

    Bạn có thể thay đổi một phần tử của một chuỗi trong Python không?

    Do đó, các chuỗi được sắp xếp các bộ sưu tập các nhân vật có thể tự nhiên.Điều này có nghĩa là bạn không thể thêm, cập nhật hoặc xóa chuỗi sau khi nó được tạo.Nếu bất kỳ hoạt động nào được thực hiện, một bản sao của chuỗi gốc sẽ được tạo và cập nhật tương ứng.Ví dụ dưới đây cho thấy chuỗi Python một cách chi tiết.you cannot add, update or delete the string after it is created. If any operations are to be performed, a copy of an original string is created and updated accordingly. The below example shows the python string in detail.

    Làm cách nào để sửa đổi chuỗi trong Python?

    Chuỗi trong Python là bất biến.Do đó, chúng tôi không thể sửa đổi chuỗi tại chỗ.we cannot modify strings in place.

    Làm thế nào để bạn thay thế một phần của chuỗi bằng một giá trị?

    Phương thức thay thế JavaScript (): Chúng ta có thể thay thế một phần chuỗi bằng cách sử dụng phương thức thay thế ().....
    Cú pháp: String.replace (SearchValue, NewValue) ....
    Ví dụ 1:.
    Đầu ra: Chuỗi ban đầu là GeekSforGeek Chuỗi được thay thế là GeekSandGeek ..