Javaexercise.com

How to format String in Python?

Python is a high-level, interpreted programming language, and is one of the most popular programming languages present in the software industry.

One of the most important but often overlooked concepts in python is that of string formatting. Let us have a look at it in detail.

What is string formatting in Python?

In Python, string formatting is basically performed to ensure that the string is displayed as expected.

We might have some parts of the string that need to be specified dynamically, for example, a hello message with some sort of a placeholder for a name.

These could be parts of the string that are not known beforehand and need to be filled in according to the manner in which the program gets executed.

Let us look at the different ways string formatting can be performed in python:

Formatting string using the % operator in Python

The % operator is also known as the modulo operator. Here, we use it to format strings to display results as desired.

Let us say we want to print a simple message to greet the user with a good morning.

We can do this with the help of a single print statement with good morning as the parameter but what if we want to have a more general format of greeting so that we can use it in the morning, afternoon and evening as well.

We do this using %s as a placeholder in the original string inside the print statement arguments followed by a % and the required greeting time.

We can store the greeting in a variable as well as shown below. 

greeting = 'morning'

print('Good %s' %greeting)

Output

Good morning

If there are multiple places where we need to format the string, we can do that using % as well.

Let us take a look at the python code and corresponding output for this method- 

print('Good %s dear %s' %('morning', 'world'))

Output

Good morning dear world

Formatting Specifiers Supported by the Python String

Formatting strings is not only limited to adding string values dynamically, but we can also add other data types as well.

For this, we need to replace %s with the placeholder for the desired data type.

The table below shows the different data types and their corresponding placeholders:

Placeholder Data Type

%s

string

%d

int

%f

float

%b

binary numbers

%o

octal

%x

hexadecimal

%e

floating point in exponential format

Formatting a string with integer variable in Python

Let us take a look at the python code and corresponding output for this method using a placeholder for int:

# Take two integer values
a = 4
b = 3

# Take a string and format it
msg = "I have %d apples and %d oranges;" %(a, b)

# Display result
print(msg)

Output

I have 4 apples and 3 oranges

Formatting string using the format() function in Python

Instead of using the % operator to format strings, we can use the format() function in a similar manner as well.

This method has a lot of visible advantages over using % operator.

The placeholders here are just curly brackets { } to refer the values.

Let us look at the python code and corresponding output for this example:

print('Hello {} {} {}'.format('How', 'Are', 'You'))

Output

Hello How Are You

Inside the placeholders, we can even mention the indices(index value) because by default the items are fit in according to the order you mentioned them in the parameters of the format() function.

Here, You is at index 2, How is at index 0, and Are is at index 1.

Let us look at the python code and corresponding output for this example:

print('Hello {2} {0} {1}'.format('How', 'Are', 'You'))

Output

Hello You How Are

Inside the placeholders, we can even mention the temporary keys so that we do not need to worry about the order of parameters inside the format() function.

Here the key a represents How, b represents Are, and c represents You.

Let us look at the python code and corresponding output for this example:

print('Hello {a} {b} {c}'.format(a = 'How', b = 'Are', c = 'You'))

Output

Hello How Are You

Conclusion

In this topic, we have learned the use and advantages of string formatting in a Python program, following some simple running examples of programs, thus giving us an intuition of how this concept could be applied in real-world situations. Feel free to reach out to info.javaexercise@gmail.com in case of any suggestions.