Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Placeholders
Numbers
Date and time
Padding and Alignment
12. String placeholders
Summary

Instruction

You did well! Now let's talk about padding and alignment; they come in handy when you want to create visually appealing reports.

By default, Python values are formatted to only take up as much space as they need. We have already seen how numbers can be defined to take up at least a given number of characters. The same is true for strings:

user_name = 'Kate'
greeting = 'Hello {:10s}!'.format(user_name)
print(greeting)

Result:

Hello Kate      !

The {:10s} placeholder means that a string value should take up at least ten characters. Kate only has four, so the remaining six were filled up with white spaces.

Exercise

Given a list of short story titles, print a list like this:

What Tears               1
The Elemental Illusion   2
...

Each title should take up at least 25 characters, followed by an ordinal number (starting at 1, not 0).

Stuck? Here's a hint!

Use a for loop in the following way:

for index in range(len(titles)):