Python tutorial- format outputs


Format outputs


To format a value as a string use %s:
>>> lname='Yuk'
>>> print '%s' % lname
Yuk
>>> 
To format a value as an integer number use %d:
>>> x=10
>>> print '%d' % x
10
To specify the minimum number of position in the outputs, you can supply width of the value:
>>> print '%10d' % x
        10
>>> 
 
>>> x=10
>>> y=15
>>> print '%-10d %d' %(x,y)
10         15
 
Note: if you supply positive width, your numbers will align right. In contrast, they will align left.
 
To format a value as a float number use %f:
>>> print '%f' % 12
12.000000
>>> 
To control precision in float number, you need to provide the number of decimal places:
>>> print '%.2f'% 12
12.00
>>> 




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.