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.
Example:
#!/usr/bin/python
Print “My name is %s and weight is %d kg!” % (‘zara’, 21)
This produce 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 %:
Format symbol
|
Conversion
|
%c
|
Character
|
%s
|
String conversion via str() prior to formatting
|
%i
|
Signed decimal integer
|
%d
|
Signed decimal integer
|
%u
|
Unsigned decimal integer
|
%o
|
Octal integer
|
%x
|
Hexadecimal integer(lowercase letters)
|
%X
|
Hexadecimal integer(Uppercase letters)
|
%e
|
Exponential notation(with lowercase ‘e’)
|
%E
|
Exponential notation(with Uppercase ‘E’)
|
%f
|
Floating point real number
|
%g
|
The shorter of %f and %e
|
%G
|
The shorter of %f and %E
|
No comments:
Post a Comment