String method startswith() in Python
The string method startswith() used to determine whether a source string starts with specified string. It returns a boolean value True, if the source string start with specified string, otherwise False.
Syntax
 1 <string>.startswith(<value>, <start>, <end>)
string : A source string to determine whether it starts with given value
value : A string to validate whether source string starts with this string
start : It specifies a starting index from where start looking into source string, optional, the default will be 0
end : It specifies an ending index to end search, Optional, the default will be string length
String method startswith()
 1 str1 = "Hello world in Python"
 2 str2 = "Welcome to IOGyan"
 3 
 4 res1 = str1.startswith('Hello')
 5 res2 = str2.startswith('Welcome')
 6 
 7 print("String start with 'Hello' :", res1)
 8 print("String start with 'Welcome' :", res2)
In the above example, a string variable is define with initial value. A startswith() method called that validates whether a source string starts with specified string. It returns boolean value True, as both strings starts with specified character sequence.
Output
 1 String start with 'Hello' : True
 2 String start with 'Welcome' : True

startswith() with start index

startswith() with start index
 1 str1 = "python program in python"
 2 str2 = "string in string"
 3 
 4 res1 = str1.startswith('python', 18)
 5 res2 = str2.startswith('string', 10)
 6 
 7 print("String start with 'python' from position 18 :", res1)
 8 print("String start with 'string' from position 10 :", res2)
In the above example, a string variables are define and initialized with string literals that includes two occurrence of string python and string. A startswith() method called by specifying a search string and start index. It validates whether a string includes specified character sequence from specified index position, as it starts with both method calls returns boolean value true.
Output
 1 String start with 'python' from position 18 : True
 2 String start with 'string' from position 10 : True

startswith() with start and end index

startswith() with start and end index
 1 str1 = "In python example"
 2 str2 = "In string example"
 3 
 4 res1 = str1.startswith('python', 3, 10)
 5 res2 = str2.startswith('string', 3, 10)
 6 
 7 print("String start with 'python' from position 3 :", res1)
 8 print("String start with 'string' from position 3 :", res2)
In the above example, a startswith() method called by specifying a search string and start and end index. It search between specified index and validates whether string starts with specified search string, as both string starts with specified value, it returns boolean value True for both method calls.
Output
 1 String start with 'python' from position 3 : True
 2 String start with 'string' from position 3 : True

startswith() with string not starts with occurrence

startswith() with string not starts with occurrence
 1 str1 = "In python example"
 2 str2 = "In string example"
 3 
 4 res1 = str1.startswith('python')
 5 res2 = str2.startswith('string', 10)
 6 
 7 print("String start with 'python' from position 0 :", res1)
 8 print("String start with 'string' from position 10 :", res2)
In the above example, a startswith() method called that search for occurrence from index 0 and index 10. As string does not starts with specified character sequence, it returns boolean value false.
Output
 1 String start with 'python' from position 0 : False
 2 String start with 'string' from position 10 : False
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us