Python String startswith() method
Python String startswith() method
The string.startswith() method in Python returns a boolean value ‘True’ if the given string starts with the prefix, otherwise it returns False.
Syntax
startswith(prefix[, start[, end]])
Parameter
prefix: This parameter signifies the value to check.
start(optional): This parameter represents an Integer specifying at which position to start the search.
end(optional): This parameter an Integer specifying at which position to end the search.
Return
This method returns a boolean value ‘True’ if the given string starts with the prefix, otherwise it returns False.
Example 1
# Python program explaining # the string.startswith() method string = "Python is a simple programming language." # substring with ‘is a simple’ result = string.startswith('is a simple') # returns False print(result) # substring with ‘Python is’ result = string.startswith('Python is ') # returns True print(result) result = string.startswith('Python is a simple programming language.') # returns True print(result)
Output
False True True
Example 2
# Python program explaining # the string.startswith() method # initializing the string string = "Hello World! Nice to meet you.."; # will return false as the string does not starts with 'world' print(string.startswith( "World")) # will return true as the substring 'lo Wor' lies between 2 to 14 print(string.startswith( "lo Wor", 3, 14 )) # will return false as the substring 'lo Wor' does not startwith 11 to 28 print(string.startswith( "Nice", 2, 4 ))
Output
False True False