Home » Python

Python String | endswith() method with example

Python String endswith() function: Here, we are going to learn endswith() function of the string in python with example.
Submitted by Yash Khandelwal, on April 14, 2019

Python String endswith() Method

endswith() Method is a library method in Python, it is used to check whether a string ends with a given suffix (substring) or not. It returns True – if the string ends with given suffix else if returns False.

Syntax:

    String.endswith(suffix, start, end)

Parameter(s):

  • suffix: This may a substring or the tuple that we are looking in the string.
  • start: It is an optional parameter in the endswith() method and it's default value is 0. It is the starting point in the string from where we want to start checking for the suffix.
  • end: It is also an optional parameter in the endswith() method and it's default value is -1. It defines the ending point in the string from where the suffix is needed within i.e. till where we want to find the suffix.

Return value:

It returns True if string ends with the given suffix otherwise returns false.

Python code to demonstrate example of String.endswith() method

# endswith() w/o start and end Parameters
print('endswith() Without start and end parameters')
string1='IncludeHelp is the Best Technical Content Website.'
suffix='Website.'
#returns True
print('When no optional parameter is passed:',string1.endswith(suffix))
#retuns False as the '.' is not inc;luded
print('When no optional parameter is passed:',string1.endswith('Website'))
#more than one word may be there but a single string is there.
print('When large string is to be found:',string1.endswith('Technical Content Website.'))

print()
print()
print()

# endswith() With start and end Parameters
print('endswith() With start and end Parameters')
# Both start and end is provided
# start: 42, end: 51 
# "programming is easy" string is searched
#returns True
print('When optional parameter is passed:',string1.endswith(suffix,42,51))
#retuns False as the '.' is not inc;luded
print('When optional parameter is passed:',string1.endswith('Website',43,52))
#more than one word may be there but a single string is there.
print('When large string is to be found with optional parameters:',string1.endswith('Technical Content Website.',42,51))


print()
print()
print()

#endswith() With Tuple Suffix
print('endswith() With Tuple Suffix')
#returns True
print('When tuple is passed without optional parameters:',string1.endswith(('content','Website.','Includehelp')))
#retuns True
print('When tuple is passed with optional parameters:',string1.endswith(('content','Website.','Includehelp'),24,51))
#returns False
print('When tuple is passed without/with optional parameters:',string1.endswith(('Technical' , 'Content', 'Website.'),24,51))

Output

endswith() Without start and end parameters
When no optional parameter is passed: True 
When no optional parameter is passed: False
When large string is to be found: True 

 
 
endswith() With start and end Parameters 
When optional parameter is passed: True
When optional parameter is passed: False 
When large string is to be found with optional parameters: False 
 
 
 
endswith() With Tuple Suffix 
When tuple is passed without optional parameters: True 
When tuple is passed with optional parameters: True
When tuple is passed without/with optional parameters: True


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.