Trong ví dụ này, chúng tôi sử dụng phương pháp cắt chuỗi bằng phương pháp
text = "ABCD"
replacement = "xy"
position = 1
number_of_characters = 2
result = text[:position] + replacement + text[position + number_of_characters:]
print[result] # AxyD
3 để thay thế The original string is : geeks5geeks The constructed string : geeks4geeks2 chữ cái [chỉ số 1-3] từ
The original string is : geeks5geeks The constructed string : geeks4geeks3 bằng
The original string is : geeks5geeks The constructed string : geeks4geeks4
Chuỗi là một kiểu dữ liệu bất biến được sử dụng để lưu trữ chuỗi ký tự Unicode. Để thay thế một chuỗi trong Python, chúng ta có nhiều cách khác nhau. Một trong những cách được sử dụng rộng rãi nhất là sử dụng chuỗi phương thức tích hợp sẵn của Python. thay thế[]
Các. Phương thức replace[] thay thế ký tự cũ bằng ký tự mới. Chúng ta cũng có thể sử dụng vòng lặp để thay thế một ký tự trong chuỗi trong Python. Phương thức cắt chuỗi cũng có thể được sử dụng để thay thế một chuỗi trong python. Mô-đun regex là mô-đun Python tích hợp có thể được sử dụng để thay thế chuỗi Python
Phạm vi
Bài viết này bao gồm các chủ đề dưới đây
- Giới thiệu về chuỗi trong Python
- Nhiều cách tiếp cận khác nhau để thay thế một ký tự trong một chuỗi
- Sử dụng phương thức thay thế Chuỗi Python []
- Thay thế một ký tự trong chuỗi bằng vòng lặp trong python
- Sử dụng phương pháp cắt chuỗi
- Thay thế Ký tự tại một vị trí nhất định trong Chuỗi bằng Danh sách
- Sử dụng mô-đun regex Python
Mỗi chủ đề được giải thích rõ ràng bằng sơ đồ và ví dụ bất cứ khi nào cần thiết
Làm cách nào để thay thế một ký tự trong một chuỗi bằng Python?
Trước khi tìm hiểu các cách tiếp cận khác nhau về cách thay thế một ký tự trong chuỗi Python, chúng ta hãy thảo luận ngắn gọn về chuỗi trong python
Chuỗi là kiểu dữ liệu không thay đổi được sử dụng để lưu trữ chuỗi ký tự Unicode. Trong Python, không có kiểu dữ liệu ký tự nên ngay cả một ký tự đơn lẻ cũng được coi là một chuỗi có độ dài là 1. Chuỗi là một trong những kiểu dữ liệu được sử dụng rộng rãi nhất của bất kỳ ngôn ngữ lập trình nào. Một chuỗi trong Python có thể dễ dàng được tạo bằng cách sử dụng dấu ngoặc kép [dấu ngoặc đơn hoặc dấu ngoặc kép]
Ví dụ. string = Scaler Chủ đề hoặc Ví dụ. chuỗi = Chủ đề Scaler
Bây giờ, chúng ta hãy lấy một ví dụ để hiểu việc thay thế một ký tự trong một chuỗi trong Python có nghĩa là gì
Cho chuỗi gốc = xấu. Ta cần thay ký tự d bằng ký tự t. Vì vậy, chuỗi mới trở thành bat
Có nhiều cách để đạt được điều này trong Python
- Sử dụng phương thức thay thế chuỗi []
- Sử dụng vòng lặp
- Sử dụng cắt chuỗi
- Sử dụng một danh sách
- Sử dụng biểu thức chính quy [regex]
Chúng ta sẽ xem xét chi tiết từng phương pháp này trong phần tiếp theo
Phương pháp 1. Thay thế chuỗi Python
Chúng ta có thể sử dụng chuỗi phương thức tích hợp sẵn của Python. replace[] để thay thế một chuỗi trong python
Hãy để chúng tôi thảo luận ngắn gọn về phương thức thay thế [] trước khi thực hiện
chuỗi. replace[] là một phương thức tích hợp được liên kết với các chuỗi trong Python. Nó thay thế tất cả các lần xuất hiện của một chuỗi con cụ thể từ chuỗi gọi phương thức và trả về một bản sao của chuỗi đã thay thế
Cú pháp của phương thức replace[] rất đơn giản
string.replace[old_string, new_string, counter]
Ở đây, old_string là chuỗi mà chúng ta muốn thay thế. new_string là chuỗi sẽ thay thế chuỗi cũ. Bộ đếm là một tham số tùy chọn cho trình thông dịch Python biết số lần old_string phải được thay thế bằng new_string
Như chúng ta đã thảo luận ở trên, chuỗi. phương thức replace[] trả về một bản sao của chuỗi đã thay đổi. Nó không thay đổi chuỗi gốc
Nếu chúng tôi không chỉ định số lần thay thế phải được thực hiện [bộ đếm không được chỉ định], n tất cả các lần xuất hiện của old_sting được thay thế bằng new_string. Tìm hiểu thêm về phương thức replace[] tại đây
Hãy để chúng tôi xem việc thực hiện
old_string = "bad" new_string = "t" resultant_string = old_string.replace["d", new_string] print[resultant_string]
đầu ra
Trong đoạn mã trên, chúng tôi đã thay thế ký tự d bằng ký tự t. Sau khi thay thế, chuỗi mới được lưu trữ trong chuỗi thay thế và chúng tôi đã in nó
Trong phần này, chúng tôi thực hiện cắt chuỗi trước, cho đến i, sau đó thêm K, sau đó thêm giá trị bài đăng, sử dụng phương pháp cắt chuỗi
Python3
The original string is : geeks5geeks The constructed string : geeks4geeks5The original string is : geeks5geeks The constructed string : geeks4geeks6The original string is : geeks5geeks The constructed string : geeks4geeks7
The original string is : geeks5geeks The constructed string : geeks4geeks8The original string is : geeks5geeks The constructed string : geeks4geeks9The original string is : geeks5geeks The constructed string : geeks4geeks0The original string is : geeks5geeks The constructed string : geeks4geeks0
The original string is : geeks5geeks The constructed string : geeks4geeks1The original string is : geeks5geeks The constructed string : geeks4geeks2______33The original string is : geeks5geeks The constructed string : geeks4geeks4The original string is : geeks5geeks The constructed string : geeks4geeks5The original string is : geeks5geeks The constructed string : geeks4geeks6The original string is : geeks5geeks The constructed string : geeks4geeks7
The original string is : geeks5geeks The constructed string : geeks4geeks8The original string is : geeks5geeks The constructed string : geeks4geeks9The original string is : geeks5geeks The constructed string : geeks4geeks0The original string is : geeks5geeks The constructed string : geeks4geeks1
The original string is : geeks5geeks The constructed string : geeks4geeks2The original string is : geeks5geeks The constructed string : geeks4geeks3The original string is : geeks5geeks The constructed string : geeks4geeks0The original string is : geeks5geeks The constructed string : geeks4geeks5
The original string is : geeks5geeks The constructed string : geeks4geeks6The original string is : geeks5geeks The constructed string : geeks4geeks7__The original string is : geeks5geeks The constructed string : geeks4geeks0The original string is : geeks5geeks The constructed string : geeks4geeks9The original string is : geeks5geeks The constructed string : geeks4geeks5The original string is : geeks5geeks The constructed string : geeks4geeks9The original string is : geeks5geeks The constructed string : geeks4geeks5The original string is : geeks5geeks The constructed string : geeks4geeks3The original string is : geeks5geeks The constructed string : geeks4geeks5The original string is : geeks5geeks The constructed string : geeks4geeks5The original string is : geeks5geeks The constructed string : geeks4geeks6
The original string is : geeks5geeks The constructed string : geeks4geeks7The original string is : geeks5geeks The constructed string : geeks4geeks2______33The original string is : geeks5geeks The constructed string : geeks4geeks50The original string is : geeks5geeks The constructed string : geeks4geeks5The original string is : geeks5geeks The constructed string : geeks4geeks6The original string is : geeks5geeks The constructed string : geeks4geeks53Đầu ra
The original string is : geeks5geeks The constructed string : geeks4geeksĐộ phức tạp về thời gian. O[n] -> cắt chuỗi
Không gian phụ. O[n]Phương pháp #2. Sử dụng biểu thức tham gia [] + trình tạo
Trong phần này, chúng tôi thực hiện nhiệm vụ kiểm tra với chỉ mục và K nối thêm có điều kiện, sử dụng biểu thức trình tạo và chuyển đổi kết quả thành chuỗi bằng cách sử dụng phép nối []
Python3
The original string is : geeks5geeks The constructed string : geeks4geeks5 The original string is : geeks5geeks The constructed string : geeks4geeks6The original string is : geeks5geeks The constructed string : geeks4geeks56
The original string is : geeks5geeks The constructed string : geeks4geeks8The original string is : geeks5geeks The constructed string : geeks4geeks9The original string is : geeks5geeks The constructed string : geeks4geeks0The original string is : geeks5geeks The constructed string : geeks4geeks0
The original string is : geeks5geeks The constructed string : geeks4geeks1The original string is : geeks5geeks The constructed string : geeks4geeks2______33The original string is : geeks5geeks The constructed string : geeks4geeks4The original string is : geeks5geeks The constructed string : geeks4geeks5The original string is : geeks5geeks The constructed string : geeks4geeks6The original string is : geeks5geeks The constructed string : geeks4geeks7
The original string is : geeks5geeks The constructed string : geeks4geeks8The original string is : geeks5geeks The constructed string : geeks4geeks9The original string is : geeks5geeks The constructed string : geeks4geeks0The original string is : geeks5geeks The constructed string : geeks4geeks1
The original string is : geeks5geeks The constructed string : geeks4geeks2The original string is : geeks5geeks The constructed string : geeks4geeks3The original string is : geeks5geeks The constructed string : geeks4geeks0The original string is : geeks5geeks The constructed string : geeks4geeks5
The original string is : geeks5geeks The constructed string : geeks4geeks6The original string is : geeks5geeks The constructed string : geeks4geeks7_______60The original string is : geeks5geeks The constructed string : geeks4geeks79The original string is : geeks5geeks The constructed string : geeks4geeks80The original string is : geeks5geeks The constructed string : geeks4geeks81The original string is : geeks5geeks The constructed string : geeks4geeks0The original string is : geeks5geeks The constructed string : geeks4geeks3The original string is : geeks5geeks The constructed string : geeks4geeks84The original string is : geeks5geeks The constructed string : geeks4geeks9The original string is : geeks5geeks The constructed string : geeks4geeks86The original string is : geeks5geeks The constructed string : geeks4geeks87The original string is : geeks5geeks The constructed string : geeks4geeks88The original string is : geeks5geeks The constructed string : geeks4geeks89The original string is : geeks5geeks The constructed string : geeks4geeks3The original string is : geeks5geeks The constructed string : geeks4geeks91The original string is : geeks5geeks The constructed string : geeks4geeks92Làm cách nào để thay thế một ký tự trong chuỗi tại một chỉ mục cụ thể trong Python?
Chúng ta có thể chuyển đổi chuỗi thành một danh sách và sau đó thay thế ký tự cũ tại chỉ mục cụ thể bằng một ký tự mới . Chúng ta cũng có thể sử dụng mô-đun regex là mô-đun Python tích hợp có thể được sử dụng để thay thế chuỗi Python. chuỗi. replace[] là một trong những cách tốt nhất để thay thế chuỗi trong python.Làm cách nào để thay thế tất cả các lần xuất hiện của một ký tự trong chuỗi bằng Python?
Phương thức thay thế[] .