×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python String endswith() Method

By IncludeHelp Last updated : December 15, 2024

Python String endswith() Method

The 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)

Parameters

  • 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.

Example 1

# 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

Example 2

str1 = "Hello, welcome to Python"
result1 = str1.endswith("Python")
print(result1)

str2 = "Hello, world!"
result2 = str2.endswith("world")
print(result2)

str3 = "Hello, Python!"
result3 = str3.endswith("Python!")
print(result3)

str4 = "Hello, Python!"
result4 = str4.endswith("python")
print(result4)

Output

True
False
True
False

Comments and Discussions!

Load comments ↻





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