Python string formatting operators
Python String Formatting Operators
One of Python’s coolest features is the string formatting operator %. This operator is specific to strings and makes up for the lack of functionality found in the C language’s printf() family of functions. Within strings, format specifications like those used in C (%d %c %f %s, etc.) are used as placeholders.
Here is a simple example −
print ("My name is %s and weight is %d kg!" % ('Zara', 21))
It will produce the following output −
My name is Zara and weight is 21 kg!
Here is a complete list of symbols that can be used with % –
Sequence Number | Format Symbols and Conversion Methods |
---|---|
1 | %c Character |
2 | %s String conversion (use str() function before formatting) |
3 | %i Signed decimal integer |
4 | %d Signed decimal integer |
5 | %u Unsigned decimal integer |
6 | %o Octal integer |
7 | %x Hexadecimal integer (lowercase letters) |
8 | %X Hexadecimal integer (uppercase letters) |
9 | %e Exponential notation (lowercase ‘e’) |
10 | %E Exponential notation (uppercase ‘E’) |
11 | %f Floating point number |
12 | %g Shorter %f and %e |
13 | %G Shorter %f and %E |
Other supported symbols and functions are listed in the table below −
Order Number | Format Symbols & Conversions |
---|---|
1 | * Parameter indicating width or precision |
2 | - Left justify |
3 | + Show sign |
4 | <sp> Place a space before positive numbers |
5 | # Add a leading zero (‘0’) for octal or a leading ‘0x’ or ‘0X’ for hexadecimal, depending on the use of ‘x’ or ‘X’. |
6 | 0 Pads with zeros on the left (instead of spaces) |
7 | % ‘%%’ preserves a single ‘%’ character |
8 | (var) Maps variables (dictionary parameters) |
9 | m.n. m is the minimum total width, n is the number of digits to display after the decimal point (if applicable) |
In the following example, name is a string and age is an integer variable. Their values are inserted into the string within the %s and %d format specifications, respectively. These symbols are woven into a tuple value before the % operator.
name="Rajesh"
age=23
print ("my name is %s and my age is %d years" % (name, age))
This will produce the following output
my name is Rajesh and my age is 23 years
You can specify the width of integer and floating-point objects. In the format string, integer objects a, b, and c will occupy a width of 5 characters. Additional spaces will be padded to the left.
a=1
b=11
c=111
print ("a=%5d b=%5d c=%5d" % (a, b, c))
This will produce the following output −
a= 1 b= 11 c= 111
In the following example, the width of the floating-point variable is specified to be 6 characters, with three digits after the decimal point.
name="Rajesh"
age=23
percent=55.50
print ("my name is %s, age %d and I have scored %6.3f percent marks" % (name, age, percent))
This will produce the following output
my name is Rajesh, age 23 and I have scored 55.500 percent marks
The width of the string can also be specified. The default alignment is right. To left-align, add a negative sign to the width.
name='TutorialsPoint'
print ('Welcome To %20s The largest Tutorials Library' % (name, ))
print ('Welcome To %-20s The largest Tutorials Library' % (name, ))
This will produce the following output −
Welcome To TutorialsPoint The largest Tutorials Library
Welcome To TutorialsPoint The largest Tutorials Library
Add “.” to the format to truncate longer strings.
name='TutorialsPoint'
print ('Welcome To %.5s The largest Tutorials Library' % (name, ))
It will produce the following output −
Welcome To Tutor The largest Tutorials Library