How to check if a Python string starts with a specific character

Been writing for some time a side project in the Python computer programming language, there was a case when I had to check if a string object started with a specific character or not. Then based on the status of the condition, I had to make sure to run different branches of the project’s code.

The good thing is that the Python computer programming language is so rich in features, that almost anything can be accomplished by making use of builtin utilities. According to my personal experience with the Python scripting language, the string object has a specific builtin method which can be used to check if it starts with a specific character or not.

Before going any further with this tutorial, make sure to launch a new Python interactive console so you can make sure to practice the theory by yourself.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

Once you have managed to launch a new interactive shell, declare a Python string object like shown in the following example.

s = ‘’

Then use the startswith method of the Python string object to check if the string starts with a specific character or not. The example shown below illustrates how to achieve it.


Once the above pice of Python code gets executed in my interactive console, the following output gets displayed, informing the user that the strings starts with the specific character they’re checking.


The type of the variable returned is a Bool. You can also use the condition below to check if a Python string object starts with a specific character or not.

s.startswith(‘s’) == True

The result of the above Python command is shown below.


Final thoughts

The string object is a very important Python tool to know about, especially if one has to parse output which comes as text. Personally I am writing a Python based wrapper for ffmpeg multimedia framework and I am dealing with many string objects.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Leave a Reply

Your email address will not be published. Required fields are marked *