Back

Practise formatting integers - Answers

Q1. Display 324 with a plus sign.

print("324 with the sign is {:+d}".format(324))

Q2. Display -4255 with its sign.

print("-4255 with the sign is {:+d}".format(-4255))

Q3. Display 453 as 000453.

print("453 in a field 6 characters wide, padded with zeros from the left {:0>6d}".format(453))

Q4. Display 453 as PPPPP453.

print("453 in a field 8 characters wide, padded with zeros from the left {:P>8d}".format(453))

Q5. Display 453 as 453BB.

print("453 in a field 5 characters wide, padded with zeros from the left {:B<5d}".format(453))

Q6. Display 93 in field 10 characters wide, right hand justified.

print("93 in a field 10 characters wide, right hand justified, is {:10d}".format(93))

Q7. Display 93 in field 8 characters wide, lefthand justified.

print("93 in a field 8 characters wide, right hand justified, is {:<8d}".format(93))

Q8. Display 93 in field 12 characters wide, center justified.

print("93 in a field 12 characters wide, right hand justified, is {:^12d}".format(93))

Q9. Display 69030203007 as a number separated using a comma.

print("69030203007 with comma separators is {:,}".format(69030203007))

Q10. Test each other. Write a problem, and a solution, and then give it to a neighbour for them to do. 

Students test each other with problems they make up themselves.

Back