Python f String Padding[Complete Guide with Examples]
Table of Contents
Table of Contents
In Python, f-strings are used to format strings by adding values to them. Padding is a formatting method that is used to align values when printing them in a specific way.
For example, if you want to print the table of names and their ids you can use f-strings with padding to align the values and the output look organised.
Use of zeros or spaces as padding is possible. Normally, empty spaces are added before or after values to align them, whereas leading zeros are added to numerical values by using spaces instead of zeros.
You can specify padding in an f-string by using the characters “>” and “<” specify the direction of the padding, and a number to specify the width of the padded value.
Let’s see one example, if you want to pad an employee with space to the right and an id with spaces to the left, you can use an f-string.
employee = "Alan"
id = 12
print(f"Employee: {employee:>10}, Id: {id:<10}")
Output
Employee: Alan, Id: 12
The character “>” indicates right padding for the employee with a total width of 10, while the “<” character indicates left padding for the id with the total width of 10.
Let’s see some more examples,
Python f String Padding: Right-padding with spaces
value = "Goom"
print(f"Value: {value:>10}")
Here, the variable value is written after the string “Value: ” The alignment of the value within a field of 10 characters is specified using the > character. The value will be right-aligned within a field of width 10 by using:>10.
The result of this code will therefore be:
Value: Goom
value: Goom
There will be 7 spaces before the value “Goom” because it will be right-aligned within a field that is 10 pixels wide.
Centering and using left-padding with spaces
value = "Goom"
print(f"Value: {value:^10}")
In this particular example, the string “Value: ” is printed followed by the value of the variable value. The ^ character is used to specify the alignment of the value within a field of 10 characters. By using {value:^10}, the value will be centred within a field of width 10.
So the output of this code will be:
Value: Goom
There will be 2 spaces before and 3 spaces after the value “Goom” because it will be centred within a field of width 10.
Right-padding with spaces and decimal point alignment
value = 156.356
print(f"Value: {value:>10.2f}")
In this specific illustration, the variable value is written after the string “Value: “ The alignment of the value inside a field of 10 characters is specified using the character.
Using {value:^10} will centre the value within a field that is 10 pixels wide.
The result of this code will therefore be:
Value: 156.36
There will be 4 spaces before the value 156.356, which will be right-aligned within a field that is 10 pixels wide and has 2 digits after the decimal point. The.2f format specifier directs that the value be rounded to two decimal places.
Right-padding with spaces and center-alignment for multiple values
name = "Ben"
age = 22
city = "New York"
print(f"{'Name':^10} {'Age':^10} {'City':^10}")
print(f"{name:^10} {age:^10} {city:^10}")
The values “Ben”, “22,” and “New York” correspondingly are used to define the three variables name, age, and city in this code. Next, a table with three columns is printed out: Name, Age, and City, with each column’s header being centred in a field that is 10 widths wide.
The ^ symbol in the f-strings used to format the table represents the centre alignment specifier.
Another f-string is used in the second line of the code to display the values of the name, age, and city variables, with each value centering in a field that is 10 widths wide.
The f-string contains three placeholders, {name:^10}, {age:^10}, and {city:^10}, which will be replaced with the values of the corresponding variables.
The result of the code will therefore be a table with the headers Name, Age, and City aligned in the middle with the values of the name, age, and city variables shown underneath each header, also aligned in the centres of their respective columns.
Output
Name Age City
Ben 22 New York
Here, the ^ character specifies centre-alignment, and the total width for each value is 10 with right-padding with spaces.
Conclusion
In conclusion, Python’s f-string padding is a helpful tool that lets you prepare output strings with precise widths and alignments. It operates by including special characters in an f-string’s format specifier that regulate how the value is padded within a field of a particular width.
By ensuring that your output is structured consistently, you can improve the readability and organisation of your code by utilising f-string padding. It can be used to present numerical data in tables, create structured reports, or provide output that is more aesthetically pleasing.
You can rapidly master the use of f-string padding to format your output exactly the way you want with a little practise and experimentation.