Ask coding questions

← Back to all posts
Integers of the same legth
FatimaNadeem1 (0)

Hi everyone!
I am creating a leaderboard where the top 5 scores are printed.
However, I need to make sure they are all the same length or it wouldn't sort properly.
e.g If someone score 15 and the other one scored 7, that 7 needs to be the same length so 07.
How can I code so that the lengths of the integers are the same?

Answered by Bookie0 (6559) [earned 5 cycles]
View Answer
Bookie0 (6559)

You could look at the length (len()) of the number (convert to str first), and then if it's less than 2, I guess you could add a 0 with splicing/splitting.