fbpx

String Formatting in Python

Key rings on a string

ARTICLE SUMMARY

In this guide to string formatting in Python, Indhumathy Chelliah shares the different techniques available for formatting outputs.

There are a number of different ways to format output in Python. Let’s learn about those formatting techniques in this article.

    Using string concatenation 

    Example 1: 

    By using string concatenation, we have to take care of spaces also. We are concatenating the output using + operator. 

    string formatting

     Example 2: 

    If we miss the spaces, the output is not displayed as we would expect. This is one of the drawbacks with this. 

    string formatting

    Using str.format() method 

    In str.format(), we can use {} to indicate where variable to be substituted. 

    Example 1: 

    In the below example, in the first curly brackets, the first variable ‘mango’ will be substituted and in the second curly brackets, ‘green’ will be substituted. 

    string formatting

    Example 2: 

    If we mention numbers in the curly brackets, it is used to refer to the position of the object passed to the str.format() method. 

    string formatting

    Example 3: 

    In str.format(), we can also pass the method to access the dictionary values. 

    Dictionary values can be accessed by d[key] or d.get(key) 

    string formatting

    Example 4:  

    We can mention the format specifier inside the curly brackets. It allows us to control the formatting of the values. 

    Like if we mention {:,.2f} →It indicates two decimal places. The values will be rounded to two decimal places. 

    string formatting

    Using formatted string literals (f-strings) 

    When we use f string formatting, we have to include ‘f’ before the opening quotation mark. 

    Here, we can mention expressions inside the curly brackets which will refer to variables or literal values. 

    Example 1: 

    In this example, variables are included inside the curly brackets. 

    string formatting

    Example 2: 

    In this example, a method to access dictionary values is given inside the curly brackets. 

    string formatting

    Example 3: 

    Inside the curly brackets, we can mention the string methods too. It will get evaluated and correct results will be displayed. 

    string formatting

    Example 4: 

    In this example, the expression is given inside the curly brackets {a*2}. The value of a is doubled and then the output is displayed. 

    string formatting

    Example 5: 

    Similarly, formatting the values can also be done. Here, in this example, we round off to two decimal places. 

    string formatting

    Only in the f string formatting can we include expressions. 

    Conclusion: 

    In this article, we have explored the different ways to format the output. I hope you all liked it. Thanks for reading! 

    string formatting

    AUTHOR: 

    Indhumathy Chelliah – SheCanCode Blog Squad

    RELATED ARTICLES

    Discover how a career shift from chemistry to software engineering led one professional to Quantexa's Academy Programme. Gain insights into learning Scala, overcoming technical challenges,...
    SheCanCode has partnered with coding education platform Programiz PRO to provide free subscriptions to our women in tech members.
    Join SheCanCode for a day of ideation and coding at our Financial Inclusion Power Hack! Spend the day coding solutions that will help tackle financial...
    Join SheCanCode for a day of ideation and coding at our International Women’s Day Power Hack! Spend the day coding solutions that will directly help...