Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

How to Generate Balanced Brackets in Python?

  • Isha BansalIsha Bansal
  • July 29, 2021
  • Python Programming Examples
How to Generate Balanced Brackets in Python?

In this tutorial, we will be understanding a very interesting problem known as Generate Balanced Brackets. Balanced Brackets imply that the number of opening and closing brackets are exactly equal.


Understanding the Concept of Generating Balanced Brackets

We will be handling a number of variables namely, the value of n (given by the user), output string, count of opening and closing brackets, and the iterator.

In every recursion call, the output string will be manipulated by inserting either an opening or closing bracket. And according to it, the count of opening and closing brackets is increased and the function is called recursively.

We keep checking the balancing of brackets in every recursive call.

Read more about Recursion: Recursion in Python


Generate Balanced Brackets in Python

def all_balanced(n,output,itr,count_open,count_close):

    # base case
    if(itr == 2*n):
        print(output)
        return

    # Insert open curly bracket    
    if(count_open<n):
        output = output[:itr] + '{' + output[itr+1:]
        all_balanced(n,output,itr+1,count_open+1,count_close)
    
    # Insert closing curly brackwt
    if(count_open>count_close):
        output = output[:itr] + '}' + output[itr+1:]
        all_balanced(n,output,itr+1,count_open,count_close+1)

    return

n= int(input())
all_balanced(n,"",0,0,0)

Sample Output

The output below is the result when the value of n is equal to 4. This means there will be 4 opening and 4 closing brackets.

{{{{}}}}
{{{}{}}}
{{{}}{}}
{{{}}}{}
{{}{{}}}
{{}{}{}}
{{}{}}{}
{{}}{{}}
{{}}{}{}
{}{{{}}}
{}{{}{}}
{}{{}}{}
{}{}{{}}

I hope you are clear with the concept, problem, and code implementation of the Balanced brackets problem.

Thank you for reading! Happy learning! 🙂


Isha Bansal
Isha Bansal

Hey there stranger!
Do check out my blogs if you are a keen learner!

Hope you like them!

Articles: 185
Plot data from Excel Sheet using Python
Previous Post Plot data from Excel Sheet using Python
Next Post How to Solve the Replace and Remove Problem in Python?
How to Solve the Replace and Remove Problem in Python?

Related Posts

How to Calculate Distance Between Two Points in Python

How to Calculate Distance Between Two Points in Python

  • November 26, 2026
CIFAR 10 Dataset: Load and Use in Python

CIFAR 10 Dataset: Load and Use in Python

  • November 24, 2026
Maximum A Posteriori Estimation in Python

Maximum A Posteriori Estimation in Python

  • November 24, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.