Wednesday, May 6, 2015

Python: String Formats

ESCAPEDESCRIPTIONEXAMPLE
%dDecimal integers (not floating point)."%d" % 45 == '45'
%iSame as %d."%i" % 45 == '45'
%oOctal number."%o" % 1000 == '1750'
%uUnsigned decimal."%u" % -1000 == '-1000'
%xHexadecimal lowercase."%x" % 1000 == '3e8'
%XHexadecimal uppercase."%X" % 1000 == '3E8'
%eExponential notation, lowercase 'e'."%e" % 1000 == '1.000000e+03'
%EExponential notation, uppercase 'E'."%E" % 1000 == '1.000000E+03'
%fFloating point real number."%f" % 10.34 == '10.340000'
%FSame as %f."%F" % 10.34 == '10.340000'
%gEither %f or %e, whichever is shorter."%g" % 10.34 == '10.34'
%GSame as %g but uppercase."%G" % 10.34 == '10.34'
%cCharacter format."%c" % 34 == '"'
%rRepr format (debugging format)."%r" % int == "'int'>"
%sString format."%s there" % 'hi' == 'hi there' %%A percent sign."%g%%" % 10.34 == '10.34%'



EScape



No comments: