Python Modulo String Formatting
Last Updated :
12 Mar, 2024
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.
Python3
variable = '15'
string = "Variable as string = %s" %(variable)
print (string )
print ("Variable as raw data = %r" %(variable))
variable = int(variable)
string = "Variable as integer = %d" %(variable)
print (string)
print ("Variable as float = %f" %(variable))
print ("Variable as printing with special char = %c" %(variable))
print ("Variable as hexadecimal = %x" %(variable))
print ("Variable as octal = %o" %(variable))
|
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...