0% found this document useful (0 votes)
5 views1 page

Check Parentheses Balance in Python

The document contains a Python function 'is_balanced' that checks if parentheses in a given string are balanced. It uses a stack to keep track of opening parentheses and returns True if all parentheses are properly matched and closed. The main block allows user input and prints whether the input string is balanced or not.

Uploaded by

s41227315
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Check Parentheses Balance in Python

The document contains a Python function 'is_balanced' that checks if parentheses in a given string are balanced. It uses a stack to keep track of opening parentheses and returns True if all parentheses are properly matched and closed. The main block allows user input and prints whether the input string is balanced or not.

Uploaded by

s41227315
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

def is_balanced(input_str):

s=list()
for ch in input_str:
if ch == '(':
[Link](ch)
print(s)
if ch == ')':
if not s:
return False
[Link]()
print(s)

return not s

if __name__=="__main__":
input_str=input()

if is_balanced(input_str):
print(input_str,"balanced")
else:
print(input_str,"not balanced")

You might also like