Check if String Ends with Specified Substring using endswith() Method in Python

Last updated on January 20, 2023

The endswith() method checks if the string ends with a specified substring. For example, loot at below string,

“It is nice weather today.”

In this string we need to check if it ends with “today.”. To do that, we can use endswith() function. The endswith() function has three parameters in which the first is mandatory and two are optional. It returns boolean value (true or false).

Let’s use the endswith() method for checking if the above string ends with “today.”. In the first step create a variable and store the string into the variable.

todayWeather = "It is nice weather today."

Next, we need to attach endsWith() method with the variable.

isEnd = todayWeather.endswith("today.")

After attaching the method, we need to pass the substring for checking if the string ends with the specified substring.

# Output: True

In the last part, we got boolean value (true) in output. This boolean value is returned by endsWith() method for the string and substring that we passed.

Written by
I am a skilled full-stack developer with extensive experience in creating and deploying large and small-scale applications. My expertise spans front-end and back-end technologies, along with database management and server-side programming.

Share on:

Related Posts

Tags: Python, String,