In addition to the functions described earlier, Python string variables can also use the startswith and endswith functions.
The startswith() function is used to retrieve whether the string starts with the specified string, and returns True if it is; otherwise it returns False. The syntax of this method is as follows:
str.startswith(sub[,start][,end]))
The specific meanings of the parameters in this format are as follows:
[Example:] Determine whether "www.freelearningpoints.com" starts with "w" substring.
string = "www.freelearningpoints.com" string.startswith("w")
The output is:
True
[Example:]
string = "www.freelearningpoints.com" string.startswith("a")
The output is:
False
[Example] Search from the specified position.
string = "www.freelearningpoints.com" string.startswith("f", 4)
The output is:
True
The endswith() function is used to retrieve whether the string ends with the specified string, and returns True if it is; otherwise it returns False. The syntax of the method is as follows:
str.endswith(sub[,start][,end]))
The specific meanings of the parameters in this format are as follows:
[Example:] Search whether "www.freelearningpoints.com" ends with "com".
string = "www.freelearningpoints.com" string.endswith("com")
The output is:
True
More Tutorials:
Python Installation - Linux (Ubuntu)More Python Exercises:
Python String Exercises