Các phần bên dưới nêu bật các toán tử, phương thức và hàm có sẵn để làm việc với chuỗi
Các phương thức chuỗi tích hợp
Bạn đã học trong hướng dẫn về Biến trong Python rằng Python là một ngôn ngữ hướng đối tượng cao. Mỗi mục dữ liệu trong chương trình Python là một đối tượng
Bạn cũng đã quen thuộc với các chức năng. các thủ tục có thể gọi được mà bạn có thể gọi để thực hiện các tác vụ cụ thể
Phương thức tương tự như hàm. Một phương thức là một loại thủ tục có thể gọi được chuyên biệt được liên kết chặt chẽ với một đối tượng. Giống như một hàm, một phương thức được gọi để thực hiện một tác vụ riêng biệt, nhưng nó được gọi trên một đối tượng cụ thể và có kiến thức về đối tượng đích của nó trong quá trình thực thi
Cú pháp gọi một phương thức trên một đối tượng như sau
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
73Điều này gọi phương thức
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
118 trên đối tượng >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
71. >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
120 chỉ định các đối số được truyền cho phương thức (nếu có)Bạn sẽ khám phá nhiều hơn nữa về việc định nghĩa và gọi các phương thức sau này trong phần thảo luận về lập trình hướng đối tượng. Hiện tại, mục tiêu là trình bày một số phương thức tích hợp được sử dụng phổ biến hơn mà Python hỗ trợ để thao tác trên các đối tượng chuỗi
Trong các định nghĩa phương thức sau đây, các đối số được chỉ định trong ngoặc vuông (______472) là tùy chọn
Chuyển đổi trường hợp
Các phương thức trong nhóm này thực hiện chuyển đổi trường hợp trên chuỗi mục tiêu
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
122Viết hoa chuỗi mục tiêu
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
122 trả về một bản sao của >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 với ký tự đầu tiên được chuyển thành chữ hoa và tất cả các ký tự khác được chuyển thành chữ thường>>>
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
74Các ký tự không phải là bảng chữ cái không thay đổi
>>>
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
75>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
125Chuyển đổi các ký tự chữ cái thành chữ thường
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
125 trả về một bản sao của >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 với tất cả các ký tự chữ cái được chuyển thành chữ thường>>>
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
76>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
128Trường hợp hoán đổi của các ký tự chữ cái
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
128 trả về một bản sao của >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 với các ký tự chữ hoa được chuyển thành chữ thường và ngược lại>>>
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
77>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
131Chuyển đổi chuỗi mục tiêu thành "trường hợp tiêu đề. ”
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
131 trả về một bản sao của >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 trong đó chữ cái đầu tiên của mỗi từ được chuyển đổi thành chữ hoa và các chữ cái còn lại là chữ thường>>>
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
78Phương pháp này sử dụng một thuật toán khá đơn giản. Nó không cố gắng phân biệt giữa các từ quan trọng và không quan trọng, và nó không xử lý các dấu nháy đơn, sở hữu hoặc từ viết tắt một cách duyên dáng
>>>
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
79>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
134Chuyển đổi các ký tự chữ cái thành chữ hoa
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
134 trả về một bản sao của >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 với tất cả các ký tự chữ cái được chuyển thành chữ hoa>>>
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
90Tìm và thay thế
Các phương thức này cung cấp các phương tiện khác nhau để tìm kiếm chuỗi mục tiêu cho một chuỗi con đã chỉ định
Mỗi phương thức trong nhóm này hỗ trợ các đối số
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
137 và >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
138 tùy chọn. Chúng được hiểu là để cắt chuỗi. hành động của phương thức bị giới hạn ở phần của chuỗi đích bắt đầu từ vị trí ký tự >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
137 và tiếp tục đến nhưng không bao gồm vị trí ký tự >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
138. Nếu >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
137 được chỉ định nhưng >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
138 thì không, phương pháp này sẽ áp dụng cho phần của chuỗi mục tiêu từ >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
137 đến hết chuỗi>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
144Đếm số lần xuất hiện của một chuỗi con trong chuỗi đích
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
145 trả về số lần xuất hiện không chồng lấp của chuỗi con >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
146 trong >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37>>>
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
91Số lượng được giới hạn ở số lần xuất hiện trong chuỗi con được chỉ định bởi
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
137 và >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
138, nếu chúng được chỉ định>>>
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
92>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
150Xác định xem chuỗi mục tiêu có kết thúc bằng một chuỗi con đã cho hay không
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
151 trả về >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 nếu >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 kết thúc bằng >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
154 đã chỉ định và ngược lại là >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
48>>>
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
93Việc so sánh được giới hạn ở chuỗi con được chỉ định bởi
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
137 và >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
138, nếu chúng được chỉ định>>>
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
94>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
158Tìm kiếm chuỗi mục tiêu cho một chuỗi con nhất định
Bạn có thể sử dụng
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
159 để xem liệu một chuỗi Python có chứa một chuỗi con cụ thể hay không. >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
160 trả về chỉ số thấp nhất trong >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 nơi tìm thấy chuỗi con >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
146>>>
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
95Phương thức này trả về
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
76 nếu không tìm thấy chuỗi con đã chỉ định>>>
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
96Việc tìm kiếm được giới hạn trong chuỗi con được chỉ định bởi
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
137 và >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
138, nếu chúng được chỉ định>>>
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
97>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
166Tìm kiếm chuỗi mục tiêu cho một chuỗi con nhất định
Phương pháp này giống hệt với
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
159, ngoại trừ việc nó đưa ra một ngoại lệ nếu không tìm thấy >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
146 thay vì trả về >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
76>>>
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
98>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
170Tìm kiếm chuỗi mục tiêu cho một chuỗi con nhất định bắt đầu từ cuối
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
171 trả về chỉ số cao nhất trong >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 nơi tìm thấy chuỗi con >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
146>>>
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
99Như với
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
159, nếu không tìm thấy chuỗi con, thì trả về >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
76>>>
st = "pencil programmer"
n = 7
print(f"{n}th Letter of Each Word:")
for word in st.split():
print(word[n] if n < len(word) else None)
70Việc tìm kiếm được giới hạn trong chuỗi con được chỉ định bởi
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
137 và >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
138, nếu chúng được chỉ định>>>
st = "pencil programmer"
n = 7
print(f"{n}th Letter of Each Word:")
for word in st.split():
print(word[n] if n < len(word) else None)
71>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
178Tìm kiếm chuỗi mục tiêu cho một chuỗi con nhất định bắt đầu từ cuối
Phương thức này giống hệt với
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
179, ngoại trừ việc nó đưa ra một ngoại lệ nếu không tìm thấy >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
146 thay vì trả về >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
76>>>
st = "pencil programmer"
n = 7
print(f"{n}th Letter of Each Word:")
for word in st.split():
print(word[n] if n < len(word) else None)
72>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
182Xác định xem chuỗi mục tiêu có bắt đầu bằng một chuỗi con đã cho hay không
Khi bạn sử dụng phương thức Python
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
183, >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
184 trả về >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 nếu >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 bắt đầu bằng >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
154 đã chỉ định và ngược lại là >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
48>>>
st = "pencil programmer"
n = 7
print(f"{n}th Letter of Each Word:")
for word in st.split():
print(word[n] if n < len(word) else None)
73Việc so sánh được giới hạn ở chuỗi con được chỉ định bởi
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
137 và >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
138, nếu chúng được chỉ định>>>
st = "pencil programmer"
n = 7
print(f"{n}th Letter of Each Word:")
for word in st.split():
print(word[n] if n < len(word) else None)
74Phân loại nhân vật
Các phương thức trong nhóm này phân loại một chuỗi dựa trên các ký tự mà nó chứa
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
191Xác định xem chuỗi mục tiêu có bao gồm các ký tự chữ và số hay không
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
191 trả về >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 nếu >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 không trống và tất cả các ký tự của nó là chữ và số (một chữ cái hoặc một số) và >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
48 nếu không>>>
st = "pencil programmer"
n = 7
print(f"{n}th Letter of Each Word:")
for word in st.split():
print(word[n] if n < len(word) else None)
75>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
196Xác định xem chuỗi mục tiêu có bao gồm các ký tự chữ cái hay không
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
196 trả về >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 nếu >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 không trống và tất cả các ký tự của nó là chữ cái, và >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
48 nếu ngược lại>>>
st = "pencil programmer"
n = 7
print(f"{n}th Letter of Each Word:")
for word in st.split():
print(word[n] if n < len(word) else None)
76>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
701Xác định xem chuỗi mục tiêu có bao gồm các ký tự chữ số hay không
Bạn có thể sử dụng phương thức
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
702 Python để kiểm tra xem chuỗi của bạn có chỉ gồm các chữ số không. >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
701 trả về >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 nếu >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 không trống và tất cả các ký tự của nó là các chữ số và >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
48 nếu ngược lại>>>
st = "pencil programmer"
n = 7
print(f"{n}th Letter of Each Word:")
for word in st.split():
print(word[n] if n < len(word) else None)
77>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
707Xác định xem chuỗi đích có phải là mã định danh Python hợp lệ hay không
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
707 trả về >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 nếu >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 là mã định danh Python hợp lệ theo định nghĩa ngôn ngữ và >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
48 nếu ngược lại>>>
st = "pencil programmer"
n = 7
print(f"{n}th Letter of Each Word:")
for word in st.split():
print(word[n] if n < len(word) else None)
78Ghi chú.
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
712 sẽ trả về >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 cho một chuỗi khớp với từ khóa Python mặc dù đó không thực sự là một mã định danh hợp lệ>>>
st = "pencil programmer"
n = 7
print(f"{n}th Letter of Each Word:")
for word in st.split():
print(word[n] if n < len(word) else None)
79Bạn có thể kiểm tra xem một chuỗi có khớp với từ khóa Python hay không bằng cách sử dụng hàm có tên là
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
714, hàm này nằm trong mô-đun có tên là >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
715. Một cách có thể để làm điều này được hiển thị dưới đây>>>
st = "pencil programmer"
n = 7
print(f"{n}th Letter of Each Word:")
for word in st.split():
print(word[n] if n < len(word) else None)
90Nếu bạn thực sự muốn đảm bảo rằng một chuỗi sẽ đóng vai trò là mã định danh Python hợp lệ, bạn nên kiểm tra xem
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
712 có phải là >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 không và >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
714 có phải là >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
48 khôngXem Các gói và mô-đun Python—Giới thiệu để đọc thêm về các mô-đun Python
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
720Xác định xem các ký tự chữ cái của chuỗi mục tiêu có phải là chữ thường hay không
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
720 trả về >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 nếu >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 không trống và tất cả các ký tự chữ cái chứa trong đó là chữ thường và >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
48 nếu ngược lại. Các ký tự không phải là bảng chữ cái được bỏ qua>>>
st = "pencil programmer"
n = 7
print(f"{n}th Letter of Each Word:")
for word in st.split():
print(word[n] if n < len(word) else None)
91>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
725Xác định xem chuỗi mục tiêu có bao gồm toàn bộ các ký tự có thể in được hay không
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
725 trả về >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 nếu >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 trống hoặc tất cả các ký tự chữ cái chứa trong đó đều có thể in được. Nó trả về >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
48 nếu >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 chứa ít nhất một ký tự không in được. Các ký tự không phải là bảng chữ cái được bỏ qua>>>
st = "pencil programmer"
n = 7
print(f"{n}th Letter of Each Word:")
for word in st.split():
print(word[n] if n < len(word) else None)
92Ghi chú. Đây là phương thức duy nhất của
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
731 trả về >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 nếu >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 là một chuỗi rỗng. Tất cả những cái khác trả về >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
48 cho một chuỗi rỗng>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
735Xác định xem chuỗi mục tiêu có bao gồm các ký tự khoảng trắng hay không
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
735 trả về >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 nếu >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 không trống và tất cả các ký tự đều là ký tự khoảng trắng và >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
48 nếu ngược lạiCác ký tự khoảng trắng thường gặp nhất là khoảng trắng
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
740, tab >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
741 và dòng mới >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
742>>>
st = "pencil programmer"
n = 7
print(f"{n}th Letter of Each Word:")
for word in st.split():
print(word[n] if n < len(word) else None)
93Tuy nhiên, có một vài ký tự ASCII khác đủ điều kiện là khoảng trắng và nếu bạn tính đến các ký tự Unicode, thì có khá nhiều ký tự ngoài đó
>>>
st = "pencil programmer"
n = 7
print(f"{n}th Letter of Each Word:")
for word in st.split():
print(word[n] if n < len(word) else None)
94(
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
743 và >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
744 là các chuỗi thoát cho các ký tự Trả về Vận chuyển và Nguồn cấp dữ liệu Biểu mẫu ASCII; >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
745 là chuỗi thoát cho Không gian Bốn-Em-Em Unicode. )>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
746Xác định xem chuỗi mục tiêu có phải là tiêu đề không
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
746 trả về >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 nếu >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 không trống, ký tự chữ cái đầu tiên của mỗi từ là chữ hoa và tất cả các ký tự chữ cái khác trong mỗi từ là chữ thường. Nó trả về >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
48 nếu không thì>>>
st = "pencil programmer"
n = 7
print(f"{n}th Letter of Each Word:")
for word in st.split():
print(word[n] if n < len(word) else None)
95Ghi chú. Đây là cách tài liệu Python mô tả
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
751, trong trường hợp bạn thấy điều này trực quan hơn. “Các ký tự chữ hoa chỉ có thể theo sau các ký tự không có chữ cái và các ký tự chữ thường chỉ có các ký tự có chữ hoa. ”>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
752Xác định xem các ký tự chữ cái của chuỗi mục tiêu có phải là chữ hoa hay không
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
752 trả về >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 nếu >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 không trống và tất cả các ký tự chữ cái chứa trong đó là chữ hoa và >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
48 nếu ngược lại. Các ký tự không phải là bảng chữ cái được bỏ qua>>>
st = "pencil programmer"
n = 7
print(f"{n}th Letter of Each Word:")
for word in st.split():
print(word[n] if n < len(word) else None)
96Định dạng chuỗi
Các phương thức trong nhóm này sửa đổi hoặc nâng cao định dạng của một chuỗi
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
757Căn giữa một chuỗi trong một trường
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
758 trả về một chuỗi bao gồm >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 được căn giữa trong một trường có chiều rộng >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
760. Theo mặc định, phần đệm bao gồm ký tự khoảng trắng ASCII>>>
st = "pencil programmer"
n = 7
print(f"{n}th Letter of Each Word:")
for word in st.split():
print(word[n] if n < len(word) else None)
97Nếu đối số tùy chọn
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
761 được chỉ định, nó sẽ được sử dụng làm ký tự đệm>>>
st = "pencil programmer"
n = 7
print(f"{n}th Letter of Each Word:")
for word in st.split():
print(word[n] if n < len(word) else None)
98Nếu
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 đã tồn tại ít nhất bằng >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
760, nó sẽ được trả về không thay đổi>>>
st = "pencil programmer"
n = 7
print(f"{n}th Letter of Each Word:")
for word in st.split():
print(word[n] if n < len(word) else None)
99>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
764Mở rộng các tab trong một chuỗi
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
765 thay thế từng ký tự tab (>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
741) bằng dấu cách. Theo mặc định, các khoảng trống được điền vào giả sử tab dừng ở mỗi cột thứ tám>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
60>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
767 là một tham số từ khóa tùy chọn chỉ định các cột dừng tab thay thế>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
61>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
768Căn trái một chuỗi trong trường
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
769 trả về một chuỗi bao gồm >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 được căn trái trong một trường có chiều rộng >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
760. Theo mặc định, phần đệm bao gồm ký tự khoảng trắng ASCII>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
62Nếu đối số tùy chọn
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
761 được chỉ định, nó sẽ được sử dụng làm ký tự đệm>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
63Nếu
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 đã tồn tại ít nhất bằng >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
760, nó sẽ được trả về không thay đổi>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
64>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
775Cắt các ký tự đầu từ một chuỗi
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
776 trả về một bản sao của >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 với bất kỳ ký tự khoảng trắng nào bị xóa khỏi đầu bên trái>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
65Nếu đối số
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
778 tùy chọn được chỉ định, thì đó là một chuỗi chỉ định bộ ký tự sẽ bị xóa>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
66>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
779Thay thế các lần xuất hiện của một chuỗi con trong một chuỗi
Trong Python, để xóa một ký tự khỏi chuỗi, bạn có thể sử dụng phương thức Python string
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
780. >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
781 trả về một bản sao của >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 với tất cả các lần xuất hiện của chuỗi con >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
783 được thay thế bằng >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
784>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
67Nếu đối số
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
785 tùy chọn được chỉ định, tối đa thay thế >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
785 được thực hiện, bắt đầu từ đầu bên trái của >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
68>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
788Căn phải một chuỗi trong một trường
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
789 trả về một chuỗi bao gồm >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 được căn phải trong một trường có chiều rộng >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
760. Theo mặc định, phần đệm bao gồm ký tự khoảng trắng ASCII>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
69Nếu đối số tùy chọn
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
761 được chỉ định, nó sẽ được sử dụng làm ký tự đệm>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
80Nếu
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 đã tồn tại ít nhất bằng >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
760, nó sẽ được trả về không thay đổi>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
81>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
795Cắt các ký tự theo sau từ một chuỗi
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
796 trả về một bản sao của >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 với bất kỳ ký tự khoảng trắng nào bị xóa khỏi đầu bên phải>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
82Nếu đối số
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
778 tùy chọn được chỉ định, thì đó là một chuỗi chỉ định bộ ký tự sẽ bị xóa>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
83>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
799Tách các ký tự từ đầu bên trái và bên phải của một chuỗi
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
900 về cơ bản tương đương với việc gọi liên tiếp >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
776 và >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
796. Không có đối số >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
778, nó sẽ loại bỏ khoảng trắng ở đầu và cuối>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
84Như với
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
904 và >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
905, đối số tùy chọn >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
778 chỉ định bộ ký tự sẽ bị xóa>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
85Ghi chú. Khi giá trị trả về của một phương thức chuỗi là một chuỗi khác, như thường lệ, các phương thức có thể được gọi liên tiếp bằng cách xâu chuỗi các lệnh gọi
>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
86>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
907Đệm một chuỗi ở bên trái bằng số không
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
907 trả về một bản sao của >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 được đệm bên trái với các ký tự >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
910 cho >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
760 đã chỉ định>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
87Nếu
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 chứa một dấu hiệu ở đầu, thì nó vẫn ở cạnh trái của chuỗi kết quả sau khi các số 0 được chèn vào>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
88Nếu
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 đã tồn tại ít nhất bằng >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
760, nó sẽ được trả về không thay đổi>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
89>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
915 hữu ích nhất cho các biểu diễn chuỗi số, nhưng Python vẫn vui vẻ sẽ không đệm một chuỗi không phải là>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
30Chuyển đổi giữa chuỗi và danh sách
Các phương thức trong nhóm này chuyển đổi giữa một chuỗi và một số kiểu dữ liệu tổng hợp bằng cách dán các đối tượng lại với nhau để tạo thành một chuỗi hoặc bằng cách chia một chuỗi thành nhiều phần
Các phương thức này hoạt động trên hoặc trả về các lần lặp, thuật ngữ Python chung cho một tập hợp các đối tượng tuần tự. Bạn sẽ khám phá hoạt động bên trong của iterables chi tiết hơn trong hướng dẫn sắp tới về phép lặp xác định
Nhiều phương thức trong số này trả về một danh sách hoặc một bộ. Đây là hai loại dữ liệu tổng hợp tương tự nhau, là ví dụ nguyên mẫu của các lần lặp trong Python. Chúng được đề cập trong hướng dẫn tiếp theo, vì vậy bạn sẽ sớm tìm hiểu về chúng. Cho đến lúc đó, chỉ cần nghĩ về chúng như các chuỗi giá trị. Một danh sách được đặt trong dấu ngoặc vuông (
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
72) và một bộ được đặt trong dấu ngoặc đơn (>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
917)Với phần giới thiệu đó, chúng ta hãy xem nhóm phương thức chuỗi cuối cùng này
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
918Nối các chuỗi từ một iterable
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
918 trả về chuỗi kết quả từ việc nối các đối tượng trong >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
920 được phân tách bằng >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37Lưu ý rằng
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
922 được gọi trên >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37, chuỗi dấu phân cách. >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
920 cũng phải là một chuỗi các đối tượng chuỗiMột số mã mẫu sẽ giúp làm rõ. Trong ví dụ sau, dấu tách
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 là chuỗi >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
926 và >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
920 là danh sách các giá trị chuỗi>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
31Kết quả là một chuỗi duy nhất bao gồm các đối tượng danh sách được phân tách bằng dấu phẩy
Trong ví dụ tiếp theo,
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
920 được chỉ định là một giá trị chuỗi đơn. Khi một giá trị chuỗi được sử dụng dưới dạng có thể lặp lại, nó được hiểu là danh sách các ký tự riêng lẻ của chuỗi>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
32Như vậy, kết quả của
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
929 là một chuỗi bao gồm mỗi ký tự trong >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
930 được phân tách bởi >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
931Ví dụ này không thành công vì một trong các đối tượng trong
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
920 không phải là một chuỗi>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
33Điều đó có thể được khắc phục, mặc dù
>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
34Như bạn sẽ sớm thấy, nhiều đối tượng phức hợp trong Python có thể được hiểu là các đối tượng có thể lặp lại và
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
922 đặc biệt hữu ích để tạo các chuỗi từ chúng>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
934Chia một chuỗi dựa trên dấu phân cách
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
934 chia tách >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 ở lần xuất hiện đầu tiên của chuỗi >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
937. Giá trị trả về là một bộ ba phần bao gồm- Phần của
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 trước >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
937 >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
937 chính nó- Phần của
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 sau >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
937
Dưới đây là một vài ví dụ về
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
943 đang hoạt động>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
35Nếu không tìm thấy
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
937 trong >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37, bộ trả về chứa >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 theo sau là hai chuỗi trống>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
36Nhớ. Danh sách và bộ dữ liệu được trình bày trong hướng dẫn tiếp theo
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
947Chia một chuỗi dựa trên dấu phân cách
Chức năng của
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
947 giống hệt như >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
934, ngoại trừ việc >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 được phân chia ở lần xuất hiện cuối cùng của >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
937 thay vì lần xuất hiện đầu tiên>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
952Tách một chuỗi thành một danh sách các chuỗi con
Không có đối số,
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
953 chia tách >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 thành các chuỗi con được phân tách bằng bất kỳ chuỗi khoảng trắng nào và trả về các chuỗi con dưới dạng danh sách>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
38Nếu
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
937 được chỉ định, nó được sử dụng làm dấu phân cách để tách>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
39(Nếu
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
937 được chỉ định với giá trị là >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
957, thì chuỗi được phân tách được phân tách bằng khoảng trắng, giống như thể >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
937 hoàn toàn không được chỉ định. )Khi
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
937 được cung cấp rõ ràng dưới dạng dấu phân cách, các dấu phân cách liên tiếp trong >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 được giả định để phân tách các chuỗi trống, chuỗi này sẽ được trả về>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
00Tuy nhiên, đây không phải là trường hợp khi bỏ qua
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
937. Trong trường hợp đó, các ký tự khoảng trắng liên tiếp được kết hợp thành một dấu phân cách duy nhất và danh sách kết quả sẽ không bao giờ chứa các chuỗi trống>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
01Nếu tham số từ khóa tùy chọn
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
962 được chỉ định, thì tối đa nhiều phần tách đó được thực hiện, bắt đầu từ đầu bên phải của >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
02Giá trị mặc định cho
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
962 là >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
76, có nghĩa là tất cả các phân tách có thể phải được thực hiện—giống như khi bỏ qua hoàn toàn >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
962>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
03>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
967Tách một chuỗi thành một danh sách các chuỗi con
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
968 hoạt động chính xác như >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
953, ngoại trừ nếu >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
962 được chỉ định, các phần tách được tính từ đầu bên trái của >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 thay vì đầu bên phải>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
04Nếu
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
962 không được chỉ định, thì không thể phân biệt được >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
973 và >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
974>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
975Phá vỡ một chuỗi tại ranh giới dòng
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
976 tách >>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 thành các dòng và trả về chúng trong một danh sách. Bất kỳ ký tự hoặc chuỗi ký tự nào sau đây được coi là cấu thành ranh giới dòngTrình tự thoát Character
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
978Newline>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
979Carriage Return>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
980Carriage Return + Line Feed>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
981 or >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
982Line Tabulation>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
983 or >>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
984Form Feed>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
985File Separator>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
986Group Separator>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
987Record Separator>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
988Next Line (C1 Control Code)_______2989Unicode Separator Paragraph0_______icodeDưới đây là một ví dụ sử dụng một số dấu tách dòng khác nhau
>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
05Nếu các ký tự ranh giới dòng liên tiếp xuất hiện trong chuỗi, chúng được coi là phân định các dòng trống, sẽ xuất hiện trong danh sách kết quả
>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
06Nếu đối số tùy chọn
>>> st = "pencil programmer"
>>> for s in st.split():
>>> print(s[10])
Traceback (most recent call last):
File "<string>", line 5, in <module>
IndexError: string index out of range
991 được chỉ định và là trung thực, thì ranh giới của dòng được giữ lại trong chuỗi kết quả>>>
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
07Loại bỏ các quảng cáo