How to Add Tab in Python


To add a Tab in Python you can make use of the \t escape sequence.

Let's take a look at a few examples.


Example 1:

    print("1\t2\t3")
    print("4\t5\t6")
    print("7\t8\t9")
    Output:
    1	2	3
    4 5 6
    7 8 9

Example 2:

    def print_countries_and_capitals(country_capitals):
        print("Country\t\tCapital")
        print("---------------------------------")
        for country, capital in country_capitals.items():
            print(f"{country}\t\t{capital}")
    
    country_capitals = {
        "USA": "Washington, D.C.",
        "UK": "London",
        "Germany": "Berlin",
        "France": "Paris",
        "Canada": "Ottawa",
        "China": "Beijing",
        "India": "New Delhi",
        "Japan": "Tokyo",
        "Brazil": "Brasília",
        "Russia": "Moscow"
    }
    
    print_countries_and_capitals(country_capitals)
    
    Output:
    Country		Capital
    ---------------------------------
    USA		Washington, D.C.
    UK		London
    Germany		Berlin
    France		Paris
    Canada		Ottawa
    China		Beijing
    India		New Delhi
    Japan		Tokyo
    Brazil		Brasília
    Russia		Moscow
Example - Add Tab in Python

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap