- 1

Solve plz

this code is printing no. From 1000-9999 but how can i print 0001,0002,0003,0004 -----this series to 0999? https://code.sololearn.com/cs1dtWzJR61p/?ref=app

31st May 2021, 6:24 AM
Aayush Jat
Aayush Jat - avatar
6 Réponses
+ 9
Use an f-string to print leading zeros for i in range(1000): print(f"{i:0>4}") The :0>4 pads the output with zeros to make each number 4 digits long https://code.sololearn.com/csYgTLLeAc0Q
31st May 2021, 7:02 AM
David Ashton
David Ashton - avatar
+ 4
Aayush Jat The output of the print() function is ALWAYS a string. You can't print integers.
31st May 2021, 6:53 AM
David Ashton
David Ashton - avatar
+ 4
AKSHAY🇮🇳 I'm sorry. I didn't realize that you gave that answer 6 hours before Francis Chege.
1st Jun 2021, 3:57 AM
David Ashton
David Ashton - avatar
+ 3
You can try this but type of output will be string. More details on "rjust" method which is used in the code:https://www.w3schools.com/JUMP_LINK__&&__python__&&__JUMP_LINK/ref_string_rjust.asp https://code.sololearn.com/cbI7D235NziG/?ref=app
31st May 2021, 6:31 AM
AKSHAY🇮🇳
AKSHAY🇮🇳 - avatar
+ 2
David Ashton that's already covered in my answer
1st Jun 2021, 2:42 AM
AKSHAY🇮🇳
AKSHAY🇮🇳 - avatar
+ 1
for i in range(1000): print(str(i).rjust(4, '0'))
31st May 2021, 12:15 PM
Francis Chege
Francis Chege - avatar