startswith() Function in Python – string starts with

startswith() Function in python checks whether the string starts with a specific string. There is an optional argument that specifies the beginning and end of the string.

startswith() function returns True if the string starts with the specified string . If not it returns False

 

Syntax of startswith() Function in Python:

str.startswith(str, beg,end);

str – The string to be checked

beg – optional parameter to set start index of the matching boundary.

end – optional parameter to end the index of the matching boundary.

 

Example of startswith() Function in Python:

startswith() function returns True when it starts with specified character. If not it returns True.

str1 = "Protect our earth. We have got only one!"
str2= "2020 world will turn peaceful"

print str1.startswith("Protect")
print str2.startswith("2020")
print str2.startswith("world")

output:

True
True
False

 

Example of startswith() Function in Python with beginning and end argument:

str1 = "Protect our earth. We have got only one!"

print str1.startswith("our",8,len(str1))
print str1.startswith("our",8,15)

output:

True
True

 

previous startswith() Function in python                                                                                                           next startswith() Function in python

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts