Python string startswith() Method

In this tutorial, We will see the python string startswith() method with examples.

startswith() Method

The startswith() method is one of the methods in a python string. Python string startswith() method checks whether the string is starting with a particular string(prefix) or not.

  • Return True, if the string starts with a particular value.
  • Return False, If the string does not start with a particular value.
Syntax of Python string startswith() method

This is the syntax of the python string startswith() method.

string.startswith(value)
Example of the Python string startswith() method
#Example 1 

text = "python is a programming language"
#check the text starts with py or not.  
print(text.startswith("py"))
#output : True
#Example 2

text = "python3"
#check the text starts with po or not. 
print(text.startswith("po"))

#output: False

#Exmaple 3

message = "cyber fauz"
msg = message.startswith("cyber")
print(msg)


#output: True

Program: Take a sentence from the user and take a word from the user. Check whether the sentence is starting from the given word of the user or not.

message = input("Enter sentence")
word = input("Enter Word")
msg = message.startswith(word)
print(msg)

Related Posts