Advertisement



< Prev
Next >



Python - String endswith() Method




The endswith() method available to string objects determines if a string object ends with a specific string value or not and if it does, the bool value True is returned, else the bool value False is returned.




Signature of endswith() method


endswith(value, start, end)


Parameters Description
value
This is a string value, which is searched in the invoked string. A non-optional attribute.

start
This is an integer value to specify the index position(zero-based index) to start the search at. An optional attribute.

end
An integer value to specify the index position(one-based index) to end the search at. An optional attribute.







In the upcoming example, we are going to call the endswith() with two optional parameters start and end.

# Python - Example of endswith() method.


# Creating a string 
s1 = 'The Sky'



# Checking if the string has a value ' Sky', starting at index 3 and ending at 6
# which is false because the value ' Sky' starts at index 3(zero-based index) but ends at index 7(one-based index) 
b1 = s1.endswith(' Sky', 3, 6)

print(b1)




# Checking if the string has a value 'Sky', starting at index 4 and ending at 7
# which is true because the value 'Sky' starts at index 4 and ends at index 7
b1 = s1.endswith('Sky', 4, 7)
print(b1)




# Checking if the string has a value ' Sky', starting at index 3 and ending at 7
# which is true because the value ' Sky' starts at index 3 and ends at index 7
b1 = s1.endswith(' Sky', 3, 7)

print(b1)  



# Checking if the string ends with the value 'ky', starting at index 5 and ending at 10
# which is true because the value 'ky' ends at the index 5 and ends before or at index 10
b2 = s1.endswith('ky', 5, 10)

print(b2)



# Checking if the string ends with the value 'y', starting at index 2 and ending at index 5
# which is false because the value 'y' does not start at index 2
b3 = s1.endswith('y', 2, 5)
print(b3)


Output is :


False
True
True
True
False





Please share this article -




< Prev
Next >
< String startswith() Method
String index() Method >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement