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: True
Cú 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úctext = "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úctext = "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úcVượ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 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
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
- 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ápCú 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ápCú 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.Examplemessage = 'Python is fun'
# check if the message starts with Python
print(message.startswith('Python'))
# Output: True
Syntax 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() Parameterstext = "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 ValueExample 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 Parameterstext = "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
True
Example 2: startswith() With start and end Parameterstext = "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)
Example 2: startswith() With start and end Parameters True
False
True
Passing Tuple to startswith()Example 3: startswith() With Tuple Prefix Description
Example 3: startswith() With Tuple Prefixtext = "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)
Example 2: startswith() With start and end Parameters True
False
False
Passing Tuple to startswith()
DescriptionReturn Valuestartswith() checks
whether string starts with str, optionally restricting the matching with the given indices start and end. Cú phápCú 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) 0 str.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 ValueCú pháp ExampleThe 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
|