Exercise: Check Whether the Brackets are Balanced

Let's check if the brackets in a string are balanced.

We'll cover the following

Problem statement

Given a string containing only square brackets, [], you must check whether the brackets are balanced or not. The brackets are said to be balanced if there is a closing bracket for every opening bracket.

Therefore, your task is to write the check_balance() function, which returns True if the brackets are balanced and False if they are not.

For the sake of simplicity, you can assume that the string will not contain any other characters.






This problem has been designed for you to practice freely, so try to solve it on your own first. Take some time and think about the different concepts that have been explored in the course so far.

If you feel stuck, you can always check out the solution review provided in the next lesson.

Good luck!

Get hands-on with 1200+ tech skills courses.