Information formatting

In this section, we are going to learn about string formatting. We are going to learn how to format information in two ways: one by using the string format() method and the other by using the % operator.

First, we will learn string formatting using the string format() method. This method of the string class allows us to do value formatting. It also allows us to do variable substitutions. This will concatenate the elements through positional arguments.

Now, we are going to learn how to do this formatting using formatters. The string on which this method is called can contain literal text or replacement fields delimited by braces {}. Multiple pairs of {} can be used while formatting a string. This replacement field contains either an index of an argument, or the name of argument. As a result, you will get a copy of a string where each replacement field is replaced with the string value of an argument.

Now, we will look at an example of string formatting.

Create a format_example.py script and write the following content in it:

# Using single formatter
print("{}, My name is John".format("Hi"))
str1 = "This is John. I am learning {} scripting language."
print(str1.format("Python"))

print("Hi, My name is Sara and I am {} years old !!".format(26))

# Using multiple formatters
str2 = "This is Mary {}. I work at {} Resource department. I am {} years old !!"
print(str2.format("Jacobs", "Human", 30))

print("Hello {}, Nice to meet you. I am {}.".format("Emily", "Jennifer"))

Run the script as follows:

student@ubuntu:~/work$ python3 format_example.py
Output:
Hi, My name is John
This is John. I am learning Python scripting language.
Hi, My name is Sara and I am 26 years old !!
This is Mary Jacobs. I work at Human Resource department. I am 30 years old !!
Hello Emily, Nice to meet you. I am Jennifer.

In the preceding example, we did string formatting using the format() method of string class using single and multiple formatters.

Now, we are going to learn about string formatting using the % operator. There are format symbols used with the % operator. Here are some commonly used symbols:

Now, we will look at an example. Create a string_formatting.py script and write the following content in it:

# Basic formatting
a = 10
b = 30
print("The values of a and b are %d %d" % (a, b))
c = a + b
print("The value of c is %d" % c)

str1 = 'John'
print("My name is %s" % str1)

x = 10.5
y = 33.5
z = x * y
print("The value of z is %f" % z)
print()

# aligning
name = 'Mary'
print("Normal: Hello, I am %s !!" % name)

print("Right aligned: Hello, I am %10s !!" % name)

print("Left aligned: Hello, I am %-10s !!" % name)
print()

# truncating
print("The truncated string is %.4s" % ('Examination'))
print()

# formatting placeholders
students = {'Name' : 'John', 'Address' : 'New York'}
print("Student details: Name:%(Name)s Address:%(Address)s" % students)

Run the script and you will get the output as follows:

student@ubuntu:~/work$ python3 string_formatting.py
The values of a and b are 10 30
The value of c is 40
My name is John
The value of z is 351.750000
Normal: Hello, I am Mary !!
Right aligned: Hello, I am Mary !!
Left aligned: Hello, I am Mary !!
The truncated string is Exam
Student details: Name:John Address:New York

In the preceding example, we used the % operator to format strings: %d for numbers, %s for strings, and %f for float numbers. Then, we aligned the string to the left and right. We also learned how to truncate the string using the % operator. %.4s will display only the first four characters. Next, we created a dictionary named students and entered Name and Address key value pairs. Next, we placed our key names after the % operator to get the strings.