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 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 trả 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)4 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ề 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)5.Thí dụ
message = 'Python is fun'# check if the message starts with Python print(message.startswith('Python'))
# Output: TrueCú pháp của chuỗi startSwith ()
Cú pháp của 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 là:str.startswith(prefix[, start[, end]])tham số startswith ()
Phương 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)3 mất 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 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 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 TrueVí 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 searchedresult = text.startswith('programming is', 7)
print(result) # start: 7, end: 18 # 'programming' string is searchedresult = text.startswith('programming is', 7, 18)
print(result)result = text.startswith('program', 7, 18)
print(result)Đầu ra
True False TrueVí 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 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 trong Python.Nếu chuỗi bắt đầu với bất kỳ mục nào của tuple, 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ẽ trả về đúng. Nếu không, nó trả về saitext = "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 checkedresult = text.startswith(('programming', 'easy'), 12, 19)
# prints False print(result)Đầu ra
True False FalseVí dụ 2: startswith () với tham số bắt đầu và kết thúc
Hàm startswith() trong Python xác định xem chuỗi hoặc chuỗi con (nếu bạn cung cấp chỉ mục bắt đầu begin và chỉ mục kết thúc end) có bắt đầu với chuỗi con str không, nếu có trả về true, nếu không là false.startswith() trong Python xác định xem chuỗi hoặc chuỗi con (nếu bạn cung cấp chỉ mục bắt đầu begin và chỉ mục kết thúc end) có bắt đầu với chuỗi con str không, nếu có trả về true, nếu không là false. Nội dung chínhShow
Cú pháp
Cú pháp của startswith() trong Python:startswith() trong Python:
str.startswith(str, begin=0,end=len(string));Các tham số:
str: Là chuỗi cần được kiểm tra.: Là chuỗi cần được kiểm tra.
begin: Tham số tùy ý này để thiết lập chỉ mục bắt đầu của boundary kết nối.: Tham số tùy ý này để thiết lập chỉ mục bắt đầu của boundary kết nối.
end: Tham số tùy ý này để thiết lập chỉ mục kết thúc của boundary kết nối.: Tham số tùy ý này để thiết lập chỉ mục kết thúc của boundary kết nối.
Ví dụ sau minh họa cách sử dụng của startswith() trong Python.
str1 = "Vi du ham Python startswith" print (str1.startswith('Vi')) print (str1.startswith('du', 2, 4)) print (str1.startswith('Py', 2, 4))Chạy chương trình Python trên sẽ cho kết quả:
Hàm startswith() trong Python xác định xem chuỗi hoặc chuỗi con (nếu bạn cung cấp chỉ mục bắt đầu begin và chỉ mục kết thúc end) có bắt đầu với chuỗi con str không, nếu có trả về true, nếu không là false.startswith() trong Python xác định xem chuỗi hoặc chuỗi con (nếu bạn cung cấp chỉ mục bắt đầu begin và chỉ mục kết thúc end) có bắt đầu với chuỗi con str không, nếu có trả về true, nếu không là false.
Nội dung chính
- Syntax of String startswith()
- startswith() Parameters
- startswith() Return Value
- Example 1: startswith() Without start and end Parameters
- Example 2: startswith() With start and end Parameters
- Passing Tuple to startswith()
- Example 3: startswith() With Tuple Prefix
- Description
- Return Value
Cú pháp
Cú pháp của startswith() trong Python:startswith() trong Python:
str.startswith(str, begin=0,end=len(string));Các tham số:
str: Là chuỗi cần được kiểm tra.: Là chuỗi cần được kiểm tra.
begin: Tham số tùy ý này để thiết lập chỉ mục bắt đầu của boundary kết nối.: Tham số tùy ý này để thiết lập chỉ mục bắt đầu của boundary kết nối.
end: Tham số tùy ý này để thiết lập chỉ mục kết thúc của boundary kết nối.: Tham số tùy ý này để thiết lập chỉ mục kết thúc của boundary kết nối.
Ví dụ sau minh họa cách sử dụng của startswith() trong Python.
str1 = "Vi du ham Python startswith" print (str1.startswith('Vi')) print (str1.startswith('du', 2, 4)) print (str1.startswith('Py', 2, 4))Chạy chương trình Python trên sẽ cho kết quả:
In this tutorial, we will learn about the Python String startswith() method with the help of examples.
The 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 method returns 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)4 if a string starts with the specified prefix(string). If not, it returns 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)5.Example
message = 'Python is fun'# check if the message starts with Python print(message.startswith('Python'))
# Output: TrueSyntax of String startswith()
The syntax of 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 is:str.startswith(prefix[, start[, end]])startswith() Parameters
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 method takes a maximum of three parameters:- prefix - String or tuple of strings to be checked - String or tuple of strings to be checked
- start (optional) - Beginning position where prefix is to be checked within the string. (optional) - Beginning position where prefix is to be checked within the string.
- end (optional) - Ending position where prefix is to be checked within the string. (optional) - Ending position where prefix is to be checked within the string.
startswith() Return Value
Example 1: startswith() Without start and end Parameters
- Example 2: startswith() With start and end Parameters
- Passing Tuple to startswith()
Example 1: startswith() Without start and end Parameters
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)Example 2: startswith() With start and end Parameters
False True TrueExample 2: startswith() With start and end Parameters
text = "Python programming is easy." # start parameter: 7 # 'programming is easy.' string is searchedresult = text.startswith('programming is', 7)
print(result) # start: 7, end: 18 # 'programming' string is searchedresult = text.startswith('programming is', 7, 18)
print(result)result = text.startswith('program', 7, 18)
print(result)Example 2: startswith() With start and end Parameters
True False TruePassing Tuple to startswith()
Example 3: startswith() With Tuple Prefix
Description
Example 3: startswith() With Tuple Prefix
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 checkedresult = text.startswith(('programming', 'easy'), 12, 19)
# prints False print(result)Example 2: startswith() With start and end Parameters
True False FalsePassing Tuple to startswith()
Description
Return Valuestartswith() checks whether string starts with str, optionally restricting the matching with the given indices start and end.
Cú pháp
Cú pháp của startswith() trong Python:startswith() method −
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)0str.startswith(str, begin=0,end=len(string));
Các tham số: − This is the string to be checked.
str: Là chuỗi cần được kiểm tra. − This is the optional parameter to set start index of the matching boundary.
begin: Tham số tùy ý này để thiết lập chỉ mục bắt đầu của boundary kết nối. − This is the optional parameter to end start index of the matching boundary.
Return Value
Cú pháp
Example
The syntax of 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 is: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)1text = "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 method takes a maximum of three parameters: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)2python_strings.htm