w3resource

Python: Check whether a string starts with specified characters


Check if string starts with specified chars.

Write a Python program to check whether a string starts with specified characters.

Python String Exercises: Check whether a string starts with specified characters

Sample Solution:

Python Code:

# Define a variable 'string' and assign it the value "w3resource.com".
string = "w3resource.com"

# Use the startswith() method to check if the string 'string' starts with the substring "w3r".
# The result is a boolean value, which is printed.
print(string.startswith("w3r"))

Sample Output:

True

For more Practice: Solve these Related Problems:

  • Write a Python program to verify if a string starts with a given prefix using the startswith() method.
  • Write a Python program to compare the first few characters of a string with a specified substring using slicing.
  • Write a Python program to implement a function that returns True if a string begins with specific characters and False otherwise.
  • Write a Python program to use regular expressions to check if a string starts with certain characters.

Go to:


Previous: Write a Python program to remove a newline in Python.
Next: Write a Python program to create a Caesar encryption.

Python Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.