Hướng dẫn endswith python - tận cùng với con trăn



Hàm endswith() trong Python xác định xem nếu chuỗi string hoặc chuỗi con đã cho của string (nếu bạn cung cấp chỉ mục bắt đầu beg và chỉ mục kết thúc end) kết thúc với hậu tố suffix thì trả về true, nếu không thì phương thức này trả về false. xác định xem nếu chuỗi string hoặc chuỗi con đã cho của string (nếu bạn cung cấp chỉ mục bắt đầu beg và chỉ mục kết thúc end) kết thúc với hậu tố suffix thì trả về true, nếu không thì phương thức này trả về false.

Nội dung chính

  • Syntax of String endswith()
  • endswith() Parameters
  • Return Value from endswith()
  • Example 1: endswith() Without start and end Parameters
  • Example 2: endswith() With start and end Parameters
  • Passing Tuple to endswith()
  • Example 3: endswith() With Tuple Suffix
  • Phương thức chuỗi "endWith" là gì?


Cú pháp

str.endswith(suffix[, start[, end]])

Các tham số:

  • suffix: Đây có thể là một chuỗi hoặc cũng có thể là một tuple của các tiền tố.: Đây có thể là một chuỗi hoặc cũng có thể là một tuple của các tiền tố.

  • start: Chỉ mục bắt đầu.: Chỉ mục bắt đầu.

  • end: Chỉ mục kết thúc.: Chỉ mục kết thúc.



str1 = "Vi du ham endswith trong python...test";
suffix = "test";
print (str1.endswith(suffix))
print (str1.endswith(suffix, 20))
suffix = "ham"
print (str1.endswith(suffix, 2, 4))
print (str1.endswith(suffix, 2, 6))

Kết quả là:



In this tutorial, we will learn about the Python String endswith() method with the help of examples.

The

str1 = "Vi du ham endswith trong python...test";
suffix = "test";
print (str1.endswith(suffix))
print (str1.endswith(suffix, 20))
suffix = "ham"
print (str1.endswith(suffix, 2, 4))
print (str1.endswith(suffix, 2, 6))
2 method returns
str1 = "Vi du ham endswith trong python...test";
suffix = "test";
print (str1.endswith(suffix))
print (str1.endswith(suffix, 20))
suffix = "ham"
print (str1.endswith(suffix, 2, 4))
print (str1.endswith(suffix, 2, 6))
3 if a string ends with the specified suffix. If not, it returns
str1 = "Vi du ham endswith trong python...test";
suffix = "test";
print (str1.endswith(suffix))
print (str1.endswith(suffix, 20))
suffix = "ham"
print (str1.endswith(suffix, 2, 4))
print (str1.endswith(suffix, 2, 6))
4.

Example

message = 'Python is fun'

# check if the message ends with fun print(message.endswith('fun'))

# Output: True

Syntax of String endswith()

endswith() Parameters

str.endswith(suffix[, start[, end]])

endswith() Parameters

Return Value from endswith()

  • Example 1: endswith() Without start and end Parameters - String or tuple of suffixes to be checked
  • Example 2: endswith() With start and end Parameters (optional) - Beginning position where suffix is to be checked within the string.
  • Passing Tuple to endswith() (optional) - Ending position where suffix is to be checked within the string.

Return Value from endswith()

Example 1: endswith() Without start and end Parameters

  • Example 2: endswith() With start and end Parameters
  • Passing Tuple to endswith()

Example 1: endswith() Without start and end Parameters

text = "Python is easy to learn."

result = text.endswith('to learn')

# returns False print(result)

result = text.endswith('to learn.')

# returns True print(result)

result = text.endswith('Python is easy to learn.')

# returns True print(result)

Example 2: endswith() With start and end Parameters

False
True
True

Example 2: endswith() With start and end Parameters

text = "Python programming is easy to learn."

# start parameter: 7
# "programming is easy to learn." string is searched

result = text.endswith('learn.', 7)

print(result) # Both start and end is provided # start: 7, end: 26 # "programming is easy" string is searched

result = text.endswith('is', 7, 26)

# Returns False print(result)

result = text.endswith('easy', 7, 26)

# returns True print(result)

Example 2: endswith() With start and end Parameters

True
False
True

Passing Tuple to endswith()

Example 3: endswith() With Tuple Suffix

Phương thức chuỗi "endWith" là gì?


Example 3: endswith() With Tuple Suffix

text = "programming is easy"

result = text.endswith(('programming', 'python'))

# prints False print(result)

result = text.endswith(('python', 'easy', 'java'))

#prints True print(result) # With start and end parameter # 'programming is' string is checked

result = text.endswith(('is', 'an'), 0, 14)

# prints True print(result)

Example 2: endswith() With start and end Parameters

False
True
True

Passing Tuple to endswith()

Hướng dẫn endswith python - tận cùng với con trăn


Phương thức chuỗi "endWith" là gì?

Cú phápDựa trên sự so sánh này, nó sẽ trả về boolean Đúng hoặc Sai.

Cú pháp

str1 = "Vi du ham endswith trong python...test";
suffix = "test";
print (str1.endswith(suffix))
print (str1.endswith(suffix, 20))
suffix = "ham"
print (str1.endswith(suffix, 2, 4))
print (str1.endswith(suffix, 2, 6))
0

Các tham số:

suffix: Đây có thể là một chuỗi hoặc cũng có thể là một tuple của các tiền tố.

start: Chỉ mục bắt đầu.

  • end: Chỉ mục kết thúc. Chuỗi ký tự được cung cấp trong "hậu tố" KHÔNG khớp với chuỗi kết thúc của chuỗi gọi
  • Kết quả là: Chuỗi ký tự được cung cấp trong "hậu tố" khớp với chuỗi kết thúc của chuỗi gọi

In this tutorial, we will learn about the Python String endswith() method with the help of examples.

The

str1 = "Vi du ham endswith trong python...test";
suffix = "test";
print (str1.endswith(suffix))
print (str1.endswith(suffix, 20))
suffix = "ham"
print (str1.endswith(suffix, 2, 4))
print (str1.endswith(suffix, 2, 6))
2 method returns
str1 = "Vi du ham endswith trong python...test";
suffix = "test";
print (str1.endswith(suffix))
print (str1.endswith(suffix, 20))
suffix = "ham"
print (str1.endswith(suffix, 2, 4))
print (str1.endswith(suffix, 2, 6))
3 if a string ends with the specified suffix. If not, it returns
str1 = "Vi du ham endswith trong python...test";
suffix = "test";
print (str1.endswith(suffix))
print (str1.endswith(suffix, 20))
suffix = "ham"
print (str1.endswith(suffix, 2, 4))
print (str1.endswith(suffix, 2, 6))
4.

Example

str1 = "Vi du ham endswith trong python...test";
suffix = "test";
print (str1.endswith(suffix))
print (str1.endswith(suffix, 20))
suffix = "ham"
print (str1.endswith(suffix, 2, 4))
print (str1.endswith(suffix, 2, 6))
1

The syntax of

str1 = "Vi du ham endswith trong python...test";
suffix = "test";
print (str1.endswith(suffix))
print (str1.endswith(suffix, 20))
suffix = "ham"
print (str1.endswith(suffix, 2, 4))
print (str1.endswith(suffix, 2, 6))
2 is:

The

str1 = "Vi du ham endswith trong python...test";
suffix = "test";
print (str1.endswith(suffix))
print (str1.endswith(suffix, 20))
suffix = "ham"
print (str1.endswith(suffix, 2, 4))
print (str1.endswith(suffix, 2, 6))
2 takes three parameters:
EndsWith character 'ple': true
EndsWith character 'Java': false

suffix - String or tuple of suffixes to be checked