Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Python String startswith() Function

String in Python has built-in functions for almost every action to be performed on a string. Python String startswith() function checks for the specific prefix in a string and returns True else False.

Key Points:

  • Return Type: Boolean i.e. True or False
  • Parametric Values: There are 3 parameters: Prefix, Start, End
PrefixIt can be a String or Tuple of Strings that are to be checked. It is
Case Sensitive
StartIt is optional and is to specify the starting index from where the check will start
EndIt is optional and is to specify the ending index where the check will end

Python String startswith() Syntax

str_name.startswith()

str_name here refers to the string in which prefix is to be checked and strartwith() is an inbuilt function.


String startswith() Examples

Example 1:

text_string = "Engineering and Management are altogether diferent verticals."

resultant = text_string.startswith('Engineering')

print(resultant)

resultant = text_string.startswith('Management ')

print(resultant)

Output :

Output Startswith Example1
Output-startswith() Example1

Example 2:

text_string = "Engineering and Management are altogether different verticals."

resultant = text_string.startswith('Engineering',0)

print(resultant)

resultant = text_string.startswith('Management',16,60)

print(resultant)

Output:

Output Startswith Example2
Output-startswith() Example2

Example 3:

str = "Engineering Discipline"

string=str.startswith("Discipline")

print (string)

Output:

False

Conclusion

In this article, we have understood the working and implementation of substring() function under various circumstances of inputs.

References