- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
String Formatting Operator in Python
One of Python's coolest features is the string format operator %. This operator is unique to strings and makes up for the pack of having functions from C's printf() family. Following is a simple example −
#!/usr/bin/python print "My name is %s and weight is %d kg!" % ('Zara', 21)
When the above code is executed, it produces the following result −
My name is Zara and weight is 21 kg!
Here is the list of complete set of symbols which can be used along with % −
|Sr.No||Format Symbol & Conversion|
string conversion via str() prior to formatting
signed decimal integer
signed decimal integer
unsigned decimal integer
hexadecimal integer (lowercase letters)
hexadecimal integer (UPPERcase letters)
exponential notation (with lowercase 'e')
exponential notation (with UPPERcase 'E')
floating point real number
the shorter of %f and %e
the shorter of %f and %E
Other supported symbols and functionality are listed in the following table −
|Sr.No||Symbol & Functionality|
argument specifies width or precision
display the sign
leave a blank space before a positive number
add the octal leading zero ( '0' ) or hexadecimal leading '0x' or '0X', depending on whether 'x' or 'X' were used.
pad from left with zeros (instead of spaces)
'%%' leaves you with a single literal '%'
mapping variable (dictionary arguments)
m is the minimum total width and n is the number of digits to display after the decimal point (if appl.)
- String Formatting in Python using %?
- String Formatting in C# using %
- String Formatting in Java using %
- How to compare Python string formatting: % with .format?
- Python - Output Formatting
- JSON Formatting in Python
- String Formatting with ToString in C#
- How to print a complete tuple in Python using string formatting?
- Generic output formatting in Python
- String Formatting in C# to add padding
- How does string formatting work in PowerShell?
- String Concatenation by + (string concatenation) operator.
- String Formatting in C# to add padding on the right
- String compare by == operator in Java