Python startswith and Python endswith Function


In this tutorial, we will learn about Python startswith and Python endswith Function. In python programming, startswith() is a function used for check that the string starts from the specific prefix, and endswith() function is used to check that the string is ending with the specified suffix. The startswith() and endswith() function returns the boolean value ( True or False). python string starts with and python string ends with takes three parameters.

Python startswith and Python endswith Function

Python startswith() Function

In python string methods, the startswith() function is used to check that the given string is starts with the specific prefix substring. It returns the boolean value. It takes three parameter, prefix, start_index, end_index. Where start_index and end_index are optional. If the optional parameter are not included in the startswith() function, then the searching start from index 0 to index -1.

Syntax of Python String startswith()  :  string.startswith(prefix, start_index, end_index)

Example of Python startswith Function:

python string startswith method

Code

#given string
string = 'Technoelearn'

#check the prefix by using startswith()
result = string.startswith('Techno', 0, -1)

print(result)
Output : True

As you can see in the above example, the startswith() function is used to check the given string is starts with a specific prefix. By using startswith() the result became, True because the substring ‘Techno’ is the prefix of the given string ‘Technoelearn’.


Python endswith() Function

In the python string methods, the python endswith() function is used to check the string is ending with the given string. If the string is ending with the specified suffix, the return True otherwise returns False. It takes three parameters suffix, start_index, and end_index. Whereas start_index and end_index are optional and if not included then the string starts at index 0 to index -1.

Syntax of Python String endswith()  :  string.endswith(suffix, start_index, end_index)

Example of Python endswith Function:

python string endswith method

Code

#string
string = 'Technoelearn'

#Use endswith() function
result = string.endswith('learn', 1)

print(result)
Output : True

As you can see in the above example, The endswith() function is used to check the given string is ending with specific substring. And the result became True because in the given string ‘Technoelearn’ the ending with the specific suffix ‘learn’ which is true.

Video Tutorial for Python startswith Function

https://www.youtube.com/watch?v=ZTdLTaqaX0k

Video Tutorial for Python endswith Function

Learn More Python Tutorial:

If you have any queries regarding python startswith and python endswith function, then ask on the comment section, I am happy to answer your question

1 thought on “Python startswith and Python endswith Function”

  1. naturally like your website however you need to take a look at the spelling on several
    of your posts. A number of them are rife with spelling issues and I find it very troublesome to inform the truth on the other hand I’ll certainly come again again.

    Reply

Leave a Comment

Share via
Copy link
Powered by Social Snap