Bài viết này mô tả cách thay thế các chuỗi trong Python.
- Thay thế Subrings:
8print[s.replace['one', 'XXX']] # XXX two XXX two XXX print[s.replace['one', 'XXX', 2]] # XXX two XXX two one
- Cách sử dụng cơ bản
- Chỉ định số lượng thay thế tối đa:
9print[s.replace['one', 'XXX']] # XXX two XXX two XXX print[s.replace['one', 'XXX', 2]] # XXX two XXX two one
- Thay thế nhiều chuỗi con khác nhau
- Chuỗi hoán đổi
- Thay thế ký tự mới
- Thay thế nhiều ký tự khác nhau:
0print[s.replace['one', 'XXX'].replace['two', 'YYY']] # XXX YYY XXX YYY XXX
- Cách sử dụng cơ bản
- Chỉ định số lượng thay thế tối đa:
9print[s.replace['one', 'XXX']] # XXX two XXX two XXX print[s.replace['one', 'XXX', 2]] # XXX two XXX two one
- Thay thế nhiều chuỗi con khác nhau
- Cách sử dụng cơ bản
- Chỉ định số lượng thay thế tối đa:
9print[s.replace['one', 'XXX']] # XXX two XXX two XXX print[s.replace['one', 'XXX', 2]] # XXX two XXX two one
- Thay thế nhiều chuỗi con khác nhau
- Chuỗi hoán đổi
- Thay thế ký tự mới
Thay thế nhiều ký tự khác nhau:
print[s.replace['one', 'XXX'].replace['two', 'YYY']]
# XXX YYY XXX YYY XXX
0- Trao đổi ký tự
Thay thế bằng regex:
print[s.replace['one', 'XXX'].replace['two', 'YYY']]
# XXX YYY XXX YYY XXX
1, print[s.replace['one', 'XXX'].replace['two', 'YYY']]
# XXX YYY XXX YYY XXX
2
- Thay thế nhiều chuỗi con bằng cùng một chuỗi
Thay thế Subrings: print[s.replace['one', 'XXX']]
# XXX two XXX two XXX
print[s.replace['one', 'XXX', 2]]
# XXX two XXX two one
8
print[s.replace['one', 'XXX']]
# XXX two XXX two XXX
print[s.replace['one', 'XXX', 2]]
# XXX two XXX two one
Cách sử dụng cơ bản
Chỉ định số lượng thay thế tối đa:
print[s.replace['one', 'XXX']]
# XXX two XXX two XXX
print[s.replace['one', 'XXX', 2]]
# XXX two XXX two one
9- Thay thế nhiều chuỗi con khác nhau
Chuỗi hoán đổi
s = 'one two one two one'
print[s.replace[' ', '-']]
# one-two-one-two-one
Thay thế ký tự mới
print[s.replace[' ', '']]
# onetwoonetwoone
Chỉ định số lượng thay thế tối đa: print[s.replace['one', 'XXX']]
# XXX two XXX two XXX
print[s.replace['one', 'XXX', 2]]
# XXX two XXX two one
9
print[s.replace['one', 'XXX']]
# XXX two XXX two XXX
print[s.replace['one', 'XXX', 2]]
# XXX two XXX two one
Thay thế nhiều chuỗi con khác nhau
print[s.replace['one', 'XXX']]
# XXX two XXX two XXX
print[s.replace['one', 'XXX', 2]]
# XXX two XXX two one
Thay thế nhiều chuỗi con khác nhau
Chuỗi hoán đổi
Thay thế ký tự mới
print[s.replace['one', 'XXX'].replace['two', 'YYY']]
# XXX YYY XXX YYY XXX
Thay thế nhiều ký tự khác nhau:
print[s.replace['one', 'XXX'].replace['two', 'YYY']]
# XXX YYY XXX YYY XXX
0print[s.replace['one', 'XtwoX'].replace['two', 'YYY']]
# XYYYX YYY XYYYX YYY XYYYX
print[s.replace['two', 'YYY'].replace['one', 'XtwoX']]
# XtwoX YYY XtwoX YYY XtwoX
Trao đổi ký tự
Chuỗi hoán đổi
Thay thế ký tự mới
print[s.replace['one', 'two'].replace['two', 'one']]
# one one one one one
Thay thế nhiều ký tự khác nhau:
print[s.replace['one', 'XXX'].replace['two', 'YYY']]
# XXX YYY XXX YYY XXX
0print[s.replace['one', 'X'].replace['two', 'one'].replace['X', 'two']]
# two one two one two
Trao đổi ký tự
def swap_str[s_org, s1, s2, temp='*q@w-e~r^']:
return s_org.replace[s1, temp].replace[s2, s1].replace[temp, s2]
print[swap_str[s, 'one', 'two']]
# two one two one two
Thay thế bằng regex:
print[s.replace['one', 'XXX'].replace['two', 'YYY']]
# XXX YYY XXX YYY XXX
1, print[s.replace['one', 'XXX'].replace['two', 'YYY']]
# XXX YYY XXX YYY XXX
2Thay thế nhiều chuỗi con bằng cùng một chuỗi
Thay thế ký tự mới
Thay thế nhiều ký tự khác nhau:
print[s.replace['one', 'XXX'].replace['two', 'YYY']]
# XXX YYY XXX YYY XXX
0s_lines = 'one\ntwo\nthree'
print[s_lines]
# one
# two
# three
print[s_lines.replace['\n', '-']]
# one-two-three
Trao đổi ký tự
Thay thế bằng regex:
print[s.replace['one', 'XXX'].replace['two', 'YYY']]
# XXX YYY XXX YYY XXX
1, print[s.replace['one', 'XXX'].replace['two', 'YYY']]
# XXX YYY XXX YYY XXX
2print[s_lines_multi.splitlines[]]
# ['one', 'two', 'three']
print['-'.join[s_lines_multi.splitlines[]]]
# one-two-three
Thay thế nhiều chuỗi con bằng cùng một chuỗi
- Thay thế bằng cách sử dụng phần phù hợp
Thay thế nhiều ký tự khác nhau: print[s.replace['one', 'XXX'].replace['two', 'YYY']]
# XXX YYY XXX YYY XXX
0
print[s.replace['one', 'XXX'].replace['two', 'YYY']]
# XXX YYY XXX YYY XXX
Cách sử dụng cơ bản
Trao đổi ký tự
- Thay thế bằng regex:
1,print[s.replace['one', 'XXX'].replace['two', 'YYY']] # XXX YYY XXX YYY XXX
2print[s.replace['one', 'XXX'].replace['two', 'YYY']] # XXX YYY XXX YYY XXX
- Thay thế nhiều chuỗi con bằng cùng một chuỗi
Thay thế bằng cách sử dụng phần phù hợp
Nhận số lượng các bộ phận thay thế
print[s.replace[' ', '']]
# onetwoonetwoone
0Thay thế theo vị trí: Cắt lát
print[s.replace[' ', '']]
# onetwoonetwoone
1Bạn cũng có thể loại bỏ chuỗi con bằng cách thay thế nó bằng một chuỗi trống
print[s.replace['one', 'XXX'].replace['two', 'YYY']]
# XXX YYY XXX YYY XXX
3.print[s.replace[' ', '']]
# onetwoonetwoone
2Chỉ định số lượng thay thế tối đa: print[s.replace['one', 'XXX']]
# XXX two XXX two XXX
print[s.replace['one', 'XXX', 2]]
# XXX two XXX two one
9
print[s.replace['one', 'XXX']]
# XXX two XXX two XXX
print[s.replace['one', 'XXX', 2]]
# XXX two XXX two one
Thay thế nhiều chuỗi con khác nhau
print[s.replace[' ', '']]
# onetwoonetwoone
3Thay thế nhiều chuỗi con khác nhau
Chuỗi hoán đổi
- Thay thế ký tự mới
Cách sử dụng cơ bản
Trong
print[s.replace['one', 'XXX'].replace['two', 'YYY']]
# XXX YYY XXX YYY XXX
1, chỉ định mẫu regex trong đối số đầu tiên, một chuỗi mới trong chuỗi thứ hai và một chuỗi được xử lý trong chuỗi thứ ba.print[s.replace[' ', '']]
# onetwoonetwoone
4Như với
print[s.replace['one', 'XXX']]
# XXX two XXX two XXX
print[s.replace['one', 'XXX', 2]]
# XXX two XXX two one
8, bạn có thể chỉ định số lượng thay thế tối đa trong tham số thứ tư, print[s.replace['one', 'XXX']]
# XXX two XXX two XXX
print[s.replace['one', 'XXX', 2]]
# XXX two XXX two one
9.print[s.replace[' ', '']]
# onetwoonetwoone
5Bạn cũng có thể tạo một đối tượng mẫu biểu thức thông thường với
s_lines = 'one\ntwo\nthree'
print[s_lines]
# one
# two
# three
print[s_lines.replace['\n', '-']]
# one-two-three
0 và gọi phương thức def swap_str[s_org, s1, s2, temp='*q@w-e~r^']:
return s_org.replace[s1, temp].replace[s2, s1].replace[temp, s2]
print[swap_str[s, 'one', 'two']]
# two one two one two
6. Điều này hiệu quả hơn nếu bạn muốn sử dụng cùng một mẫu biểu thức thông thường nhiều lần.print[s.replace[' ', '']]
# onetwoonetwoone
6Thay thế nhiều chuỗi con bằng cùng một chuỗi
Hai cái sau đây rất hữu ích để nhớ ngay cả khi bạn không quen thuộc với Regex.
Kèm theo một chuỗi với
s_lines = 'one\ntwo\nthree'
print[s_lines]
# one
# two
# three
print[s_lines.replace['\n', '-']]
# one-two-three
2 để phù hợp với bất kỳ ký tự nào trong đó. Bạn có thể thay thế nhiều ký tự khác nhau bằng cùng một chuỗi.print[s.replace[' ', '']]
# onetwoonetwoone
7Nếu các mẫu được phân định bởi
s_lines = 'one\ntwo\nthree'
print[s_lines]
# one
# two
# three
print[s_lines.replace['\n', '-']]
# one-two-three
3, nó phù hợp với bất kỳ mẫu nào. Tất nhiên, có thể sử dụng các ký tự đặc biệt của biểu thức chính quy cho từng mẫu, nhưng nó ổn ngay cả khi chuỗi thông thường được chỉ định như vậy. Bạn có thể thay thế nhiều chuỗi khác nhau bằng cùng một chuỗi.print[s.replace[' ', '']]
# onetwoonetwoone
8Thay thế bằng cách sử dụng phần phù hợp
Nếu một phần của mẫu được đặt trong
s_lines = 'one\ntwo\nthree'
print[s_lines]
# one
# two
# three
print[s_lines.replace['\n', '-']]
# one-two-three
4, bạn có thể sử dụng một chuỗi phù hợp với phần được đặt trong s_lines = 'one\ntwo\nthree'
print[s_lines]
# one
# two
# three
print[s_lines.replace['\n', '-']]
# one-two-three
4 trong chuỗi mới.print[s.replace[' ', '']]
# onetwoonetwoone
9Cần phải thoát
s_lines = 'one\ntwo\nthree'
print[s_lines]
# one
# two
# three
print[s_lines.replace['\n', '-']]
# one-two-three
6 như s_lines = 'one\ntwo\nthree'
print[s_lines]
# one
# two
# three
print[s_lines.replace['\n', '-']]
# one-two-three
7 trong một chuỗi bình thường [print[s.replace['one', 'XXX'].replace['two', 'YYY']]
# XXX YYY XXX YYY XXX
3 hoặc s_lines = 'one\ntwo\nthree'
print[s_lines]
# one
# two
# three
print[s_lines.replace['\n', '-']]
# one-two-three
9], nhưng bạn có thể viết print[s_lines_multi.splitlines[]]
# ['one', 'two', 'three']
print['-'.join[s_lines_multi.splitlines[]]]
# one-two-three
0 trong một chuỗi thô [print[s_lines_multi.splitlines[]]
# ['one', 'two', 'three']
print['-'.join[s_lines_multi.splitlines[]]]
# one-two-three
1 hoặc print[s_lines_multi.splitlines[]]
# ['one', 'two', 'three']
print['-'.join[s_lines_multi.splitlines[]]]
# one-two-three
2].- Dây thô trong Python
Bạn có thể chỉ định một hàm, lấy một đối tượng khớp làm đối số của nó, là đối số thứ hai của
def swap_str[s_org, s1, s2, temp='*q@w-e~r^']:
return s_org.replace[s1, temp].replace[s2, s1].replace[temp, s2]
print[swap_str[s, 'one', 'two']]
# two one two one two
6. Điều này cho phép các hoạt động phức tạp hơn.print[s.replace['one', 'XXX']]
# XXX two XXX two XXX
print[s.replace['one', 'XXX', 2]]
# XXX two XXX two one
0Bạn có thể sử dụng biểu thức Lambda.
- Biểu cảm lambda trong python
print[s.replace['one', 'XXX']]
# XXX two XXX two XXX
print[s.replace['one', 'XXX', 2]]
# XXX two XXX two one
1Nhận số lượng các bộ phận thay thế
print[s.replace['one', 'XXX'].replace['two', 'YYY']]
# XXX YYY XXX YYY XXX
2 Trả về một bộ của chuỗi thay thế và số lượng các bộ phận được thay thế.- Re.subn [] - Hoạt động biểu thức thường xuyên - Python 3.10.8 Tài liệu
print[s.replace['one', 'XXX']]
# XXX two XXX two XXX
print[s.replace['one', 'XXX', 2]]
# XXX two XXX two one
2Việc sử dụng
print[s_lines_multi.splitlines[]]
# ['one', 'two', 'three']
print['-'.join[s_lines_multi.splitlines[]]]
# one-two-three
5 giống như def swap_str[s_org, s1, s2, temp='*q@w-e~r^']:
return s_org.replace[s1, temp].replace[s2, s1].replace[temp, s2]
print[swap_str[s, 'one', 'two']]
# two one two one two
6. Bạn có thể sử dụng phần được nhóm bởi s_lines = 'one\ntwo\nthree'
print[s_lines]
# one
# two
# three
print[s_lines.replace['\n', '-']]
# one-two-three
4 hoặc chỉ định số lượng thay thế tối đa.print[s.replace['one', 'XXX']]
# XXX two XXX two XXX
print[s.replace['one', 'XXX', 2]]
# XXX two XXX two one
3Thay thế theo vị trí: Cắt lát
Không có phương pháp để thay thế chuỗi ở vị trí được chỉ định.
Bằng cách chia chuỗi bằng một lát và nối chúng với một chuỗi khác, bạn có thể tạo một chuỗi mới với vị trí được chỉ định được thay thế.
print[s.replace['one', 'XXX']]
# XXX two XXX two XXX
print[s.replace['one', 'XXX', 2]]
# XXX two XXX two one
4Độ dài của chuỗi [số lượng ký tự] có thể được lấy bằng
print[s_lines_multi.splitlines[]]
# ['one', 'two', 'three']
print['-'.join[s_lines_multi.splitlines[]]]
# one-two-three
8, do đó nó có thể được viết như sau:- Nhận độ dài của một chuỗi [số lượng ký tự] trong Python
print[s.replace['one', 'XXX']]
# XXX two XXX two XXX
print[s.replace['one', 'XXX', 2]]
# XXX two XXX two one
5Số lượng ký tự không phải khớp, vì nó chỉ nối các chuỗi khác nhau giữa các chuỗi phân chia.
print[s.replace['one', 'XXX']]
# XXX two XXX two XXX
print[s.replace['one', 'XXX', 2]]
# XXX two XXX two one
6Bạn cũng có thể tạo một chuỗi mới bằng cách chèn một chuỗi khác ở bất cứ đâu trong chuỗi.
print[s.replace['one', 'XXX']]
# XXX two XXX two XXX
print[s.replace['one', 'XXX', 2]]
# XXX two XXX two one
7
Xem bài viết sau đây để biết chi tiết về cắt lát.
- Cách cắt một danh sách, chuỗi, tuple trong Python