Python | String startswith()

The startswith() method returns True if a string starts with the given prefix otherwise returns False.

Syntax :

str.startswith(prefix, start, end)

Parameters :

prefix : prefix ix nothing but a string which needs to be checked.
start : Starting position where prefix is needs to be checked within the string.
end : Ending position where prefix is needs to be checked within the string.

NOTE : If start and end index is not provided then by default it takes 0 and length-1 as starting and ending indexes where ending indes is not included in our search.

Returns :

It returns True if strings starts with the given
prefix otherwise returns False.

Examples:

Input : text = "geeks for geeks."
        result = text.startswith('for geeks')
Output : False

Input : text = "geeks for geeks."
        result = text.startswith('geeks', 0)
Output : True

Error : ValueError : This error is raised in the case when the argument string is not found in the target string

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code shows the working of
# .startsswith() function
   
text = "geeks for geeks."
   
# returns False
result = text.startswith('for geeks')
print (result)
   
# returns True
result = text.startswith('geeks')
print (result)
   
# returns False
result = text.startswith('for geeks.')
print (result)
   
# returns True
result = text.startswith('geeks for geeks.')
print (result)

chevron_right


Output:

False
True
False
True


My Personal Notes arrow_drop_up

pawanasipugmailcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.