Pythondex
Python Programs

Check If String Contains Brackets In Python

Last updated June 21, 2023 by Jarvis Silva

In this tutorial we will see how to check if a string contains brackets in python, to check whether there is a bracket in a string we will use if statement and check if there is a bracket in the string so let’s see it in code.

Python Code To Check If String Contains Brackets


text = "Hello {  how are you"

# Enter type of bracket you want check (), [], {}
if '{' in text:
    print("there is a bracket in text")

Above is the code for checking if string contains brackets, we have used the if statement to check there is a bracket in the text if it is there we print there is a bracket.

Now to run this program you need to have python installed on your computer, If you don’t have then follow this guide: Install and setup python on your computer or use this online python compiler.

After running this program you will see that it printed that there is a bracket in the string, As you can see in the program we entered a bracket In the if statement you can enter any type of bracket you want to check for example (),[],{}.

Here are some more python program tutorials for you:

I hope you found what you were looking for from this tutorial, and if you want more python programs and tutorials like this, do join our Telegram channel for future updates.

Thanks for reading, have a nice day 🙂