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

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.

Related Posts

Tags: Python, String,