Trong hướng dẫn này, chúng tôi sẽ tìm hiểu về phương thức String String STARTSWITH [] với sự trợ giúp của các ví dụ.
Phương thức
str.startswith[prefix[, start[, end]]]0 trả về
str.startswith[prefix[, start[, end]]]1 nếu một chuỗi bắt đầu với tiền tố được chỉ định [chuỗi]. Nếu không, nó trả về
str.startswith[prefix[, start[, end]]]2.
Thí dụ
message = 'Python is fun'
# check if the message starts with Python
print[message.startswith['Python']]
# Output: True
Cú pháp của chuỗi startSwith []
Cú pháp của
str.startswith[prefix[, start[, end]]]0 là:
str.startswith[prefix[, start[, end]]]
tham số startswith []
Phương thức
str.startswith[prefix[, start[, end]]]0 có tối đa ba tham số:
- Tiền tố - Chuỗi hoặc bộ của chuỗi cần kiểm tra - String or tuple of strings to be checked
- Bắt đầu [Tùy chọn] - Vị trí bắt đầu trong đó tiền tố sẽ được kiểm tra trong chuỗi. [optional] - Beginning position where prefix is to be checked within the string.
- Kết thúc [Tùy chọn] - Vị trí kết thúc trong đó tiền tố sẽ được kiểm tra trong chuỗi. [optional] - Ending position where prefix is to be checked within the string.
startswith [] giá trị trả về
Phương pháp
str.startswith[prefix[, start[, end]]]0 Trả về một boolean.
- Nó trả về true nếu chuỗi bắt đầu với tiền tố được chỉ định.
- Nó trả về sai nếu chuỗi không bắt đầu với tiền tố được chỉ định.
Ví dụ 1: startswith [] không có tham số bắt đầu và kết thúc
text = "Python is easy to learn."
result = text.startswith['is easy']
# returns False
print[result]
result = text.startswith['Python is ']
# returns True
print[result]
result = text.startswith['Python is easy to learn.']
# returns True
print[result]
Đầu ra
False True True
Ví dụ 2: startswith [] với tham số bắt đầu và kết thúc
text = "Python programming is easy."
# start parameter: 7
# 'programming is easy.' string is searched
result = text.startswith['programming is', 7]
print[result]
# start: 7, end: 18
# 'programming' string is searched
result = text.startswith['programming is', 7, 18]
print[result]
result = text.startswith['program', 7, 18]
print[result]
Đầu ra
True False True
Ví dụ 2: startswith [] với tham số bắt đầu và kết thúc
Vượt qua Tuple cho StartSwith []
Có thể chuyển một bộ tiền tố cho phương thức
str.startswith[prefix[, start[, end]]]0 trong Python.
Nếu chuỗi bắt đầu với bất kỳ mục nào của tuple, str.startswith[prefix[, start[, end]]]
0 sẽ trả về đúng. Nếu không, nó trả về sai
text = "programming is easy"
result = text.startswith[['python', 'programming']]
# prints True
print[result]
result = text.startswith[['is', 'easy', 'java']]
# prints False
print[result]
# With start and end parameter
# 'is easy' string is checked
result = text.startswith[['programming', 'easy'], 12, 19]
# prints False
print[result]
Đầu ra
True False False
Ví dụ 2: startswith [] với tham số bắt đầu và kết thúc
Tôi đã thực hiện một thử nghiệm nhỏ để xem phương pháp nào trong số này
str.startswith[prefix[, start[, end]]]
8str.startswith[prefix[, start[, end]]]
9
0text = "Python is easy to learn."
result = text.startswith['is easy']
# returns False print[result]result = text.startswith['Python is ']
# returns True print[result]result = text.startswith['Python is easy to learn.']
# returns True print[result]
1text = "Python is easy to learn."
result = text.startswith['is easy']
# returns False print[result]result = text.startswith['Python is ']
result = text.startswith['Python is easy to learn.']
# returns True print[result]
hiệu quả nhất để trả về xem một chuỗi nhất định bắt đầu bằng một chuỗi khác.
Đây là kết quả của một trong nhiều lần thử nghiệm mà tôi đã thực hiện, trong đó mỗi danh sách được yêu cầu hiển thị ít nhất thời gian [trong vài giây] để phân tích 5 triệu của mỗi biểu thức trên trong mỗi lần lặp của vòng lặp
text = "Python is easy to learn."
result = text.startswith['is easy']
# returns False
print[result]
result = text.startswith['Python is ']
# returns True
print[result]
result = text.startswith['Python is easy to learn.']
# returns True
print[result]
2 đã sử dụng:________số 8Tôi tin rằng điều khá rõ ràng ngay từ đầu rằng phương pháp
text = "Python is easy to learn."
result = text.startswith['is easy']
# returns False
print[result]
result = text.startswith['Python is ']
# returns True
print[result]
result = text.startswith['Python is easy to learn.']
# returns True
print[result]
3 sẽ xuất hiện hiệu quả nhất, vì trả về liệu một chuỗi bắt đầu với chuỗi được chỉ định là mục đích chính của nó.Điều làm tôi ngạc nhiên là phương pháp
text = "Python is easy to learn."
result = text.startswith['is easy']
# returns False
print[result]
result = text.startswith['Python is ']
# returns True
print[result]
result = text.startswith['Python is easy to learn.']
# returns True
print[result]
0 dường như không thực tế luôn tìm cách được liệt kê trước, trước phương pháp str.startswith[prefix[, start[, end]]]8, mọi lúc mọi nơi. Kết quả cho thấy rằng sử dụng
text = "Python is easy to learn."
result = text.startswith['is easy']
# returns False
print[result]
result = text.startswith['Python is ']
# returns True
print[result]
result = text.startswith['Python is easy to learn.']
# returns True
print[result]
6 để xác định xem một chuỗi bắt đầu với một chuỗi khác có hiệu quả hơn hay không thì sử dụng cả text = "Python is easy to learn."
result = text.startswith['is easy']
# returns False
print[result]
result = text.startswith['Python is ']
# returns True
print[result]
result = text.startswith['Python is easy to learn.']
# returns True
print[result]
7 và text = "Python is easy to learn."
result = text.startswith['is easy']
# returns False
print[result]
result = text.startswith['Python is ']
# returns True
print[result]
result = text.startswith['Python is easy to learn.']
# returns True
print[result]
8.Một điều nữa tôi nhận thấy là
str.startswith[prefix[, start[, end]]]9 và
text = "Python is easy to learn."
result = text.startswith['is easy']
# returns False
print[result]
result = text.startswith['Python is ']
# returns True
print[result]
result = text.startswith['Python is easy to learn.']
# returns True
print[result]
1 có một trận chiến tốt đang diễn ra, mỗi lần tăng từ vị trí thứ tư sang vị trí thứ ba, và giảm từ vị trí thứ ba xuống vị trí thứ tư, điều này có ý nghĩa, vì mục đích chính của họ là như nhau.Đây là mã:
from time import perf_counter
string = 'hello world'
places = dict[]
while True:
start = perf_counter[]
for _ in range[5000000]:
string.startswith['hello']
end = perf_counter[]
places['startswith'] = round[end - start, 2]
start = perf_counter[]
for _ in range[5000000]:
string.rfind['hello'] == 0
end = perf_counter[]
places['rfind'] = round[end - start, 2]
start = perf_counter[]
for _ in range[5000000]:
string.rpartition['hello'][0] == ''
end = perf_counter[]
places['rpartition'] = round[end - start, 2]
start = perf_counter[]
for _ in range[5000000]:
string.rindex['hello'] == 0
end = perf_counter[]
places['rindex'] = round[end - start, 2]
print[[f'{b}: {str[a].ljust[4, "4"]}' for a, b in sorted[i[::-1] for i in places.items[]]]]