Python String Formatting

Python String Formatting

There are not one but three different default ways to format strings in Python. In this article, you will understand all of them one by one.

You will also learn to pick up the best formatting option for your own program.

So, let’s begin with the C-style string formatting style.

The percent '%' operator

If you have worked with the C language before and the printf function, you will recognize and understand this method of string formatting instantly.

Python string has built-in operations that can be accessed with the ‘%‘ operator. It is great for simple positional formatting easily and quickly

Example

Python String Formatting - The percent operator - 1

Here the percent ‘%s‘ operator told Python where to put the age variable in the string.

Now, because the percent ‘%‘ operator take only one operand. But if you like to have multiple substitutions in the string, we have to slightly change the approach of formatting.

Example

Python String Formatting - The percent operator - 2

All the % variables are wrapped up in a tuple.

If you do not want to be dependent on the order of the variables, variable substitutions can also be referred by name in the format string.

Example

Python String Formatting - The percent operator - 3

This way it becomes easier to maintain the code later on when you have to make changes  in the string.

This method is  little old and the disadvantage of using it is that it requires more typing compared to the other methods.

Python format() Method

This is the new method to format strings and it was introduced with Python 3. This method allows you to format string without using the ‘%‘ operator.

To use this, you just have to call format() function using a dot separator on a string.

Format() vs % method: Format() method in Python works in a similar way to that of the percent ‘%’ operator method. The only difference is that it makes the syntax much more streamlined.

Instead of using the ‘%’ operator, this method uses curly brackets ‘{}‘ as the placeholder for the replacement in the string. Here’s an example:

Example

Python String Formatting - Python format() method - 1

Multiple Arguments

Multiple replacements can also be made for multiple placeholders. The replacements will be considered for the placeholders by the order. This is very similar to the C-style method.

Example

Python String Formatting - Python format() method - Multiple Arguments - 1

Example

Python String Formatting - Python format() method - Multiple Arguments - 2

You forgot to introduce one of your friends, this was expected, wasn’t it?

Positional Placeholders

If the placeholders ‘{}’ are empty, Python will pass the argument values in the order the placeholders were put in.

Now, let’s say you do not want to pass the values for the placeholder in the right order. Well, this can also be done.

To do this, you can cover up all the arguments in a Tuple. As you know, the index starts from 0 in a Tuple. This index number can be passed into the placeholder, so that Python can know which arguments goes in which placeholder.

Example

Python String Formatting - Python format() method - Positional Placeholders

As you can see, the string is printed in the way we intended it to be.

Substitution Type

More types can be substituted in the placeholders if we specify the type. This can be done with the below syntax.

Syntax:{index:substitution_type}

substitution_type is the type of the object we want to replace in the placeholder.

Example

Python String Formatting - Python format() method - Substitution Type

The type of the replacement that you specified gets converted to the str  type and then gets concatenated into our string.

Here are the other conversion codes.

c – Character

d – Decimal format

b – Binary format

e, E – Lowercase and uppercase exponential format

x, X – Lowercase and uppercase Hexadecimal format

o – Octal format

f – Floating point conversion

Spaces and Padding

The strings can be organized in a neater way by specifying the spacing and padding in placeholders. Python uses left-justification for strings and right-justification for numbers by default.

However, this can be altered using the code given below:

>  right-align text

< left-align text

^  center-align

=  align sign to the leftmost position

Syntax: {index:number_of_spaces}

Here are few examples:

Example

Python String Formatting - Python format() method - Spaces and Padding

In the last example, you can see that the sign “” got placed on the leftmost position from the number.

f-string method

This method was introduced in Python 3.6 update. Formally, this method is called Literal String Interpolation.

It uses the character ‘f‘ as a prefix to the string we want to format, that’s why it got the common name f-string method.

Let’s have a look on how this method works:

Example

Python String Formatting - f-string method - 1

As you can see this method directly converts the numbers into strings, making the code writing process a little simpler.

Using this method, you can also do arithmetic inline.

Example

Python String Formatting - f-string method - 2

Summary

We have covered 3 methods for the task of string formatting in Python but which is the best? Well, between the format() and String Interpolation method for string formatting, you can use anything you want, it all comes down to your preference and need.

However, as the format() method is simpler and gets rid of the percent ‘%’ operator, it should always be preferred over the C-style approach.

Tutorials for all brains!