How to format strings in Python
The output of a string can be formatted and styled to decide which data goes where and how it should be displayed.
1. Single place holder
This is the implementation used to format or style a single string.
Syntax
The general syntax is :
{}.format(value)
Where the value can be an integer, floating point numeric constant, string, character or even a variable.
print("Educative has a {} course.".format("Python Programming"))
2. Multiple place holder
Multiple strings can also be formatted and their positions defined.
Syntax
The general syntax is :
{} {}.format(value1,value2)
where value1 and value2 can be an integer, floating point numeric constant, string, character or even a variable.
Specify positional index
We can pass these index numbers into the curly braces that serve as the placeholders in the original string:
{0} {1}.format(value1,value2)
where 0 and 1 are the positions of value1 and value2 in the string, respectively.
print("Educative website has a course on {} and {} language!".format("Python", "Java"))# specify positional indexprint("Educative website has a course on {1} and {0} language!".format("Python", "Java"))
3. Conversion method
We can convert the value to different data types using the formatting method:
The general syntax is :
{ index: datatype } .format(value)
Here datatype can be d, i, f for double, integer, float
respectively, and index is the position of the value in the string if multiple values are being formatted
Note: If you try to convert the value into the data type it is originally, you will get an error. Always try converting into a different data type.
The following example converts an integer to float:
print("Employee at Educative ate {0:f} percent of a {1}!".format(75, "pizza"))
4. Padding
Any value (integer, number, string or character) can be padded from left and right.
Syntax
The general syntax is:
{start:end} .format(value)
Here start and end define the positional index of the value to begin and finish the padding.
For example,
{0:4} .format(40)
Here the value is 40. The padding will start at 4. It will count 4 as 0, 0 as 1 and then prints 3 spaces and the remaining string.
print("{0:4}, is the computer science portal for {1:10}!".format("Educative", "students"))print("It is {0:5} degrees outside !".format(40))
5. Alignment
By default, strings are left-aligned within the field, and numbers are right-aligned. Three types of alignment positions are possible:
- <: left-align text in the field
- ^: center text in the field
- > : right-align text in the field
Syntax
The general syntax is:
{start:align_type end} .format(value)
Here start and end define the positional index of the value to start and end the padding and align_type can be <, ^, >.
# To demonstrate aligning of spaces# center align Python and left align 1989print("{0:^16} implementation began in {1:<4}!".format("Python", 1989))
6. Organize large data
Formatting helps to organize large data. The following code helps to print multiples of a number within a range.
-
In the first loop, from line 2 to 3, the values are simply printed without any formatting being used.
-
The second loop, from line 5 to 6, the values are being formatted and then displayed. For each element,
count,count*2and so on, the value is formatted such that each element is right-aligned by 7 and is a number.
print("Before Formatting")for count in range (1, 10): # output without formattingprint ( count, count*2, count*3, count*4,count*5 )print("After Formatting")for count in range (1, 10): # Using formatters to give 7 spaces to each set of valuesprint("{:7d} {:7d} {:7d} {:7d}".format(count, count * 2, count * 3, count * 4,count*5))
Free Resources