Hướng dẫn what is endswith () in python? - endwith () trong python là gì?

Trong hướng dẫn này, chúng tôi sẽ tìm hiểu về phương thức chuỗi python () với sự trợ giúp của các ví dụ.

Show

Phương thức

str.endswith(suffix[, start[, end]])
2 trả về
str.endswith(suffix[, start[, end]])
3 nếu một chuỗi kết thúc với hậu tố được chỉ định. Nếu không, nó trả về
str.endswith(suffix[, start[, end]])
4.

Thí dụ

message = 'Python is fun'

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

# Output: True


Cú pháp của chuỗi endswith ()

Cú pháp của

str.endswith(suffix[, start[, end]])
2 là:

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

Các tham số endswith ()

str.endswith(suffix[, start[, end]])
2 có ba tham số:

  • Hậu tố - Chuỗi hoặc bộ tải hậu tố cần kiểm tra - String or tuple of suffixes to be checked
  • Bắt đầu (Tùy chọn) - Vị trí bắt đầu trong đó hậu tố sẽ được kiểm tra trong chuỗi. (optional) - Beginning position where suffix is to be checked within the string.
  • Kết thúc (Tùy chọn) - Vị trí kết thúc trong đó hậu tố sẽ được kiểm tra trong chuỗi. (optional) - Ending position where suffix is to be checked within the string.

Trả về giá trị từ endswith ()

Phương pháp

str.endswith(suffix[, start[, end]])
2 trả về một boolean.

  • Nó trả về true nếu một chuỗi kết thúc với hậu tố được chỉ định.
  • Nó trả về sai nếu một chuỗi không kết thúc với hậu tố được chỉ định.

Ví dụ 1: endswith () không có tham số bắt đầu và kết thúc

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)

Đầu ra

False
True
True

Ví dụ 2: endswith () với tham số bắt đầu và kết thúc

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)

Đầu ra

True
False
True

Ví dụ 2: endswith () với tham số bắt đầu và kết thúc

Chuyển tuple cho endswith ()

Có thể truyền một hậu tố tuple cho phương thức

str.endswith(suffix[, start[, end]])
2 trong Python.


Nếu chuỗi kết thúc bằng bất kỳ mục nào của tuple, str.endswith(suffix[, start[, end]])2 sẽ trả về đúng. Nếu không, nó trả về sai

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)

Đầu ra

False
True
True

Ví dụ 2: endswith () với tham số bắt đầu và kết thúc

Phương thức chuỗi Java EndSwith () được sử dụng để kiểm tra xem một chuỗi kết thúc bằng một hậu tố đã cho hay không. Nếu chuỗi kết thúc bằng một hậu tố đã cho, nó sẽ trả về đúng thì nó sẽ trả về sai.

Startswith và Endswith trong Python là gì?

Hàm python startSwith () kiểm tra xem một chuỗi bắt đầu bằng một chuỗi con được chỉ định.python endswith () kiểm tra xem một chuỗi kết thúc bằng một chuỗi con. Cả hai chức năng trả về đúng hoặc sai.

  • Endswith () làm gì trong java?
  • Phương thức EndSwith () kiểm tra xem một chuỗi kết thúc với (các) ký tự được chỉ định. Mẹo: Sử dụng phương thức startSwith () để kiểm tra xem một chuỗi bắt đầu với (các) ký tự được chỉ định.
  • Phương thức chuỗi Java EndSwith () được sử dụng để kiểm tra xem một chuỗi kết thúc bằng một hậu tố đã cho hay không. Nếu chuỗi kết thúc bằng một hậu tố đã cho, nó sẽ trả về đúng thì nó sẽ trả về sai.

    Startswith và Endswith trong Python là gì?

    Hàm python startSwith () kiểm tra xem một chuỗi bắt đầu bằng một chuỗi con được chỉ định.python endswith () kiểm tra xem một chuỗi kết thúc bằng một chuỗi con. Cả hai chức năng trả về đúng hoặc sai.

    Endswith () làm gì trong java? returns True if a string ends with the given suffix, otherwise returns False.

    Phương thức EndSwith () kiểm tra xem một chuỗi kết thúc với (các) ký tự được chỉ định. Mẹo: Sử dụng phương thức startSwith () để kiểm tra xem một chuỗi bắt đầu với (các) ký tự được chỉ định.

    Làm thế nào để bạn kiểm tra xem một chuỗi kết thúc bằng một python chữ cái?str.endswith(suffix, start, end)

    Parameters:  

    • Phương thức EndSwith () trả về true nếu một chuỗi kết thúc với hậu tố được chỉ định. Nếu không, nó trả về sai. Suffix is nothing but a string that needs to be checked. 
    • Xem thảo luậnStarting position from where suffix is needed to be checked within the string. 
    • Cải thiện bài viết Ending position + 1 from where suffix is needed to be checked within the string.

    Lưu bài viếtReturnsTrue if the string ends with the given suffix otherwise return False.

    Đọc If start and end index is not provided then by default it takes 0 and length -1 as starting and ending indexes where ending index is not included in our search.

    Bàn luận

    Python3

    Phương thức Python String EndSwith () trả về true nếu một chuỗi kết thúc bằng hậu tố đã cho, nếu không thì trả về sai.

    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)
    3
    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)
    4
    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)
    5
    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)
    6

    Output:

    True

    Ví dụ 1: Hoạt động của phương thức endswith () không có tham số bắt đầu và kết thúc

    Chúng ta sẽ xem xét nhiều trường hợp thử nghiệm về cách người ta có thể sử dụng phương thức chuỗi python () mà không cần tham số bắt đầu và kết thúc.

    Python

    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)
    7
    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)
    1
    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)
    9

    False
    True
    True
    0____21
    False
    True
    True
    2
    False
    True
    True
    3
    False
    True
    True
    4

    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)
    3
    False
    True
    True
    6

    False
    True
    True
    0____21
    False
    True
    True
    2
    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)
    0
    False
    True
    True
    4

    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)
    3
    False
    True
    True
    6

    False
    True
    True
    0____21
    False
    True
    True
    2
    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)
    0
    False
    True
    True
    4

    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)
    3
    False
    True
    True
    6

    False
    True
    True
    0____21
    False
    True
    True
    2
    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)
    0
    False
    True
    True
    4

    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)
    3
    False
    True
    True
    6

    Output:  

    False
    True
    True
    True

    False True True0____21 False True True2text = "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)0False True True4

    False
    True
    True
    0____21
    False
    True
    True
    2
    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)
    7
    False
    True
    True
    4

    Python

    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)
    7
    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)
    1
    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)
    9

    False
    True
    True
    0____21
    False
    True
    True
    2
    False
    True
    True
    3
    False
    True
    True
    4

    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)
    3
    False
    True
    True
    6

    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)
    3
    False
    True
    True
    6

    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)
    3
    False
    True
    True
    6

    False
    True
    True
    0
    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)
    1
    False
    True
    True
    2
    False
    True
    True
    3
    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)
    5
    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)
    6
    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)
    5
    True
    8
    False
    True
    True
    4

    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)
    3
    False
    True
    True
    6

    Output: 

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

    Ví dụ 3: Ví dụ trong thế giới thực trong đó EndSwith () được sử dụng rộng rãi

    Trong ví dụ này, chúng tôi lấy một đầu vào chuỗi từ người dùng và kiểm tra xem chuỗi đầu vào endswith ‘@geeksforgeek.org hay không, sau đó chúng tôi in hello geek, chúng tôi in không hợp lệ, một người lạ đã phát hiện ra.

    Python3

    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)
    3
    False
    True
    True
    True
    3
    False
    True
    True
    True
    4
    False
    True
    True
    4

    False
    True
    True
    True
    6
    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)
    1
    False
    True
    True
    True
    8
    False
    True
    True
    True
    3
    str.endswith(suffix[, start[, end]])
    00
    str.endswith(suffix[, start[, end]])
    01

    str.endswith(suffix[, start[, end]])
    02
    str.endswith(suffix[, start[, end]])
    03
    str.endswith(suffix[, start[, end]])
    044____105

    str.endswith(suffix[, start[, end]])
    06
    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)
    3
    False
    True
    True
    True
    3
    str.endswith(suffix[, start[, end]])
    09
    False
    True
    True
    4

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

    str.endswith(suffix[, start[, end]])
    06
    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)
    3
    False
    True
    True
    True
    3
    str.endswith(suffix[, start[, end]])
    16
    False
    True
    True
    4

    Output:

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

    Tại sao bạn sẽ sử dụng phương thức String EndSwith ()?

    Phương thức chuỗi Java EndSwith () được sử dụng để kiểm tra xem một chuỗi kết thúc bằng một hậu tố đã cho hay không.Nếu chuỗi kết thúc bằng một hậu tố đã cho, nó sẽ trả về đúng thì nó sẽ trả về sai.to check if a string ends with a given suffix or not. If the string ends with a given suffix it returns true else it returns false.

    Startswith và Endswith trong Python là gì?

    Hàm python startSwith () kiểm tra xem một chuỗi bắt đầu bằng một chuỗi con được chỉ định.python endswith () kiểm tra xem một chuỗi kết thúc bằng một chuỗi con.Cả hai chức năng trả về đúng hoặc sai. Python endswith() checks if a string ends with a substring. Both functions return True or False .

    Endswith () làm gì trong java?

    Phương thức EndSwith () kiểm tra xem một chuỗi kết thúc với (các) ký tự được chỉ định.Mẹo: Sử dụng phương thức startSwith () để kiểm tra xem một chuỗi bắt đầu với (các) ký tự được chỉ định.checks whether a string ends with the specified character(s). Tip: Use the startsWith() method to check whether a string starts with the specified character(s).

    Làm thế nào để bạn kiểm tra xem một chuỗi kết thúc bằng một python chữ cái?

    Phương thức EndSwith () trả về true nếu một chuỗi kết thúc với hậu tố được chỉ định.Nếu không, nó trả về sai.. If not, it returns False .