String Formatting in Python using %
In Python a string of required formatting can be achieved by different methods.
Some of them are ;
1) Using %
2) Using {}
3) Using Template Strings
In this article the formatting using % is discussed.
The formatting using % is similar to that of ‘printf’ in C programming language.
%d – integer
%f – float
%s – string
%x – hexadecimal
%o – octal
The below example describes the use of formatting using % in Python
# Python program to demonstrate the use of formatting using % # Initialize variable as a string variable = '15'string = "Variable as string = %s" %(variable) print string # Printing as raw data # Thanks to Himanshu Pant for this print "Variable as raw data = %r" %(variable) # Convert the variable to integer # And perform check other formatting options variable = int(variable) # Without this the below statement # will give error. string = "Variable as integer = %d" %(variable) print string print "Variable as float = %f" %(variable) # printing as any string or char after a mark # here i use mayank as a string print "Variable as printing with special char = %cmayank" %(variable) print "Variable as hexadecimal = %x" %(variable) print "Variable as octal = %o" %(variable) |
chevron_right
filter_none
Output :
Variable as string = 15 Variable as raw data = '15' Variable as integer = 15 Variable as float = 15.000000 Variable as printing with special char = mayank Variable as hexadecimal = f Variable as octal = 17
This article is contributed by Nikhil Kumar Singh (nickzuck_007)
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Recommended Posts:
- How to write an empty function in Python - pass statement?
- Operator Functions in Python | Set 2
- Count words in a given string
- Rearrange a string so that all same characters become d distance away
- Time Functions in Python | Set-2 (Date Manipulations)
- Send mail from your Gmail account using Python
- Python – The new generation Language
- Print Single and Multiple variable in Python
- Increment and Decrement Operators in Python
- str() vs repr() in Python
- Swap two variables in one line in C/C++, Python, PHP and Java
- Generate all permutation of a set in Python
- Class or Static Variables in Python
- trunc() in Python
- Division Operators in Python



