output formatting using % in Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

output formatting using % in Python

x=123 print('The value of x %2.1d ' %x) OUTPUT: 123 But it should be ' 1' as maximum length is two and we want only precision of one

13th May 2019, 2:58 PM
harshit
harshit - avatar
3 Answers
+ 4
If you want to display the first digit of the number 123: print('The value of x = %.1s' %x)
13th May 2019, 5:56 PM
Solo
Solo - avatar
+ 1
thanks ! But when I am using %5.5 the output is 00123 ?
13th May 2019, 3:49 PM
harshit
harshit - avatar
0
~ swim ~ have a look at this.
13th May 2019, 3:38 PM
harshit
harshit - avatar