Cách in mọi thứ sau một ký tự trong python

Bài viết này mô tả cách trích xuất một chuỗi con từ một chuỗi trong Python. Bạn có thể trích xuất một chuỗi con bằng cách chỉ định vị trí và số lượng ký tự hoặc bằng các mẫu biểu thức chính quy

  • Trích xuất một chuỗi con bằng cách chỉ định vị trí và số lượng ký tự
    • Trích xuất một ký tự theo chỉ mục
    • Trích xuất một chuỗi con bằng cách cắt
    • Trích xuất dựa trên số lượng ký tự
  • Trích xuất một chuỗi con với các biểu thức chính quy.
    # print[s[5]]
    # IndexError: string index out of range
    
    # print[s[-6]]
    # IndexError: string index out of range
    
    2,
    # print[s[5]]
    # IndexError: string index out of range
    
    # print[s[-6]]
    # IndexError: string index out of range
    
    3
  • Ví dụ về mẫu biểu thức chính quy
    • Các mẫu giống như ký tự đại diện
    • Tham lam và không tham lam
    • Trích xuất một phần của mẫu có dấu ngoặc đơn
    • Khớp với bất kỳ ký tự đơn nào
    • Khớp đầu/cuối của chuỗi
    • Trích xuất theo nhiều mẫu
    • Trường hợp không nhạy cảm

Nếu bạn muốn tìm kiếm một chuỗi để lấy vị trí của một chuỗi con đã cho hoặc thay thế một chuỗi con trong một chuỗi bằng một chuỗi khác, hãy xem bài viết sau

  • Tìm kiếm một chuỗi trong Python [Kiểm tra xem có bao gồm chuỗi con không/Nhận vị trí chuỗi con]
  • Thay chuỗi trong Python [replace, translate, re. phụ, lại. phụ]

Liên kết được tài trợ

Trích xuất một chuỗi con bằng cách chỉ định vị trí và số lượng ký tự

Trích xuất một ký tự theo chỉ mục

Bạn có thể lấy một ký tự ở vị trí mong muốn bằng cách chỉ định một chỉ mục trong

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
4. Các chỉ mục bắt đầu bằng
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
5 [lập chỉ mục dựa trên số không]

s = 'abcde'

print[s[0]]
# a

print[s[4]]
# e

nguồn. str_index_slice. py

Bạn có thể chỉ định vị trí lùi với giá trị âm.

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
6 đại diện cho ký tự cuối cùng

print[s[-1]]
# e

print[s[-5]]
# a

nguồn. str_index_slice. py

Một lỗi được đưa ra nếu chỉ mục không tồn tại được chỉ định

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range

nguồn. str_index_slice. py

Trích xuất một chuỗi con bằng cách cắt

Bạn có thể trích xuất một chuỗi con trong phạm vi

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
7 với
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
8. Nếu bỏ qua
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
9, phạm vi là từ đầu và nếu bỏ qua
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
10, phạm vi sẽ kết thúc

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
1

nguồn. str_index_slice. py

Bạn cũng có thể sử dụng các giá trị âm

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
3

nguồn. str_index_slice. py

Nếu

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
11, không có lỗi nào xuất hiện và một ký tự trống
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
12 được trích xuất

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
6

nguồn. str_index_slice. py

Ngoài phạm vi được bỏ qua

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
7

nguồn. str_index_slice. py

Ngoài vị trí bắt đầu

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
9 và vị trí kết thúc
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
14, bạn có thể chỉ định gia số
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
15 như
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
16. Nếu
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
15 là âm, nó được trích xuất từ ​​​​phía sau

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
3

nguồn. str_index_slice. py

Để biết thêm thông tin về cắt lát, hãy xem bài viết sau

  • Cách cắt danh sách, chuỗi, tuple trong Python

Trích xuất dựa trên số lượng ký tự

Hàm tích hợp sẵn

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
18 trả về số lượng ký tự. Ví dụ: bạn có thể sử dụng điều này để lấy ký tự trung tâm hoặc trích xuất nửa đầu hoặc nửa sau của chuỗi bằng cách cắt

Lưu ý rằng bạn chỉ có thể chỉ định các giá trị số nguyên

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
19 cho chỉ mục
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
4 và lát cắt
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
31. Phép chia cho
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
32 gây ra lỗi vì kết quả là số dấu phẩy động
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
33

Ví dụ sau sử dụng phép chia số nguyên

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
34. Dấu thập phân bị cắt bớt

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
1

nguồn. str_index_slice. py

Trích xuất một chuỗi con với các biểu thức chính quy.
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
2,
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
3

Bạn có thể sử dụng các biểu thức chính quy với mô-đun re của thư viện chuẩn

  • lại — Các phép toán biểu thức chính quy — Python 3. 10. 4 tài liệu

Sử dụng

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
2 để trích xuất một chuỗi con khớp với mẫu biểu thức chính quy. Chỉ định mẫu biểu thức chính quy làm tham số đầu tiên và chuỗi đích làm tham số thứ hai

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
5

nguồn. str_extract_re. py

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
38 khớp với một ký tự chữ số và
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
39 khớp với một hoặc nhiều lần lặp lại của mẫu trước đó. Do đó,
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
60 khớp với một hoặc nhiều chữ số liên tiếp

Vì dấu gạch chéo ngược

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
61 được sử dụng trong các chuỗi đặc biệt của biểu thức chính quy, chẳng hạn như
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
38, nên việc sử dụng chuỗi thô sẽ thuận tiện hơn bằng cách thêm
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
63 trước
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
12 hoặc
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
65

  • Chuỗi thô trong Python

Khi một chuỗi khớp với mẫu,

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
2 trả về một đối tượng khớp. Bạn có thể lấy phần khớp dưới dạng chuỗi
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
67 bằng phương thức
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
68 của đối tượng khớp

print[s[-1]]
# e

print[s[-5]]
# a
0

nguồn. str_extract_re. py

Như trong ví dụ trên,

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
2 chỉ trả về đối tượng khớp của phần đầu tiên, ngay cả khi có nhiều phần khớp

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
3 trả về tất cả các phần phù hợp dưới dạng danh sách các chuỗi

print[s[-1]]
# e

print[s[-5]]
# a
1

nguồn. str_extract_re. py

Liên kết được tài trợ

Ví dụ về mẫu biểu thức chính quy

Phần này trình bày một số ví dụ về các mẫu biểu thức chính quy với các ký tự meta/các chuỗi đặc biệt

Các mẫu giống như ký tự đại diện

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
71 khớp với bất kỳ ký tự đơn nào ngoại trừ một dòng mới và
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
72 khớp với 0 hoặc nhiều lần lặp lại của mẫu trước đó

Ví dụ:

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
73 khớp với chuỗi bắt đầu bằng
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
74 và kết thúc bằng
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
75. Vì
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
72 khớp với số lần lặp lại bằng 0, nên nó cũng khớp với
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
77

print[s[-1]]
# e

print[s[-5]]
# a
2

nguồn. str_extract_re. py

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
39 khớp với một hoặc nhiều lần lặp lại của mẫu trước đó.
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
79 không khớp với
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
77

print[s[-1]]
# e

print[s[-5]]
# a
3

nguồn. str_extract_re. py

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
31 khớp với số không hoặc một mẫu trước đó. Trong trường hợp của
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
32, nó khớp với
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
77 và chuỗi chỉ có một ký tự giữa
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
74 và
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
75

print[s[-1]]
# e

print[s[-5]]
# a
4

nguồn. str_extract_re. py

Tham lam và không tham lam

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
72,
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
39 và
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
31 đều là những đối sánh tham lam, đối sánh càng nhiều văn bản càng tốt.
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
39,
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
10 và
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
11 là các đối sánh tối thiểu, không tham lam, đối sánh càng ít ký tự càng tốt

print[s[-1]]
# e

print[s[-5]]
# a
5

nguồn. str_extract_re. py

Trích xuất một phần của mẫu có dấu ngoặc đơn

Nếu bạn đặt một phần của mẫu biểu thức chính quy trong ngoặc đơn

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
12, bạn có thể trích xuất một chuỗi con trong phần đó

print[s[-1]]
# e

print[s[-5]]
# a
6

nguồn. str_extract_re. py

Nếu bạn muốn khớp dấu ngoặc đơn

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
12 dưới dạng ký tự, hãy thoát chúng bằng dấu gạch chéo ngược
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
61

print[s[-1]]
# e

print[s[-5]]
# a
7

nguồn. str_extract_re. py

Khớp với bất kỳ ký tự đơn nào

Kèm theo một chuỗi có

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
4 khớp với bất kỳ ký tự nào trong chuỗi

Nếu bạn kết nối các điểm mã Unicode liên tiếp với

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
16, chẳng hạn như
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
17, thì tất cả các ký tự giữa chúng đều bị che. Ví dụ:
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
17 khớp với bất kỳ ký tự nào trong bảng chữ cái viết thường

print[s[-1]]
# e

print[s[-5]]
# a
8

nguồn. str_extract_re. py

Khớp đầu/cuối của chuỗi

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
19 khớp với phần đầu của chuỗi và
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
50 khớp với phần cuối của chuỗi

print[s[-1]]
# e

print[s[-5]]
# a
9

nguồn. str_extract_re. py

Trích xuất theo nhiều mẫu

Sử dụng

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
51 để trích xuất một chuỗi con khớp với một trong nhiều mẫu. Ví dụ: đối với các mẫu biểu thức chính quy
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
52 và
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
53, bạn có thể viết
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
54

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
0

nguồn. str_extract_re. py

Trường hợp không nhạy cảm

Mô-đun re phân biệt chữ hoa chữ thường theo mặc định. Đặt đối số

# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
55 thành
# print[s[5]]
# IndexError: string index out of range

# print[s[-6]]
# IndexError: string index out of range
56 để thực hiện phân biệt chữ hoa chữ thường

Chủ Đề