Print indent isn't working as expected

Why does thought indent but myText does not?

print("30 Days Down - What did you think?")
print()
for i in range(1, 31):
  thought = input(f"Day {i}:\n")
  print()
  myText = f"You thought Day {i} was"
  print(f"{myText:>12}")
  print(f"{thought:>12}")
  print()

I suspect it’s because of the amount of padding you’re using, if you leave a longer thought, what happens?

2 Likes

The problem is that you are formatting your text to be right aligned and stating the text is 12 characters.
MyText is longer than 12, thus no spaces will be inserted as padding.
If you want to do padding the best is to use tabs.

4 Likes

This is because the alignment does not pad the string in that way, it “justifies” the string.
Consider:

f"{'abcd':>2}"
'abcd'

f"{'abcd':>6}"
'  abcd'

The number is the length of the formatted string (if the number is already less than the length of string, nothing happens). The number is not the amount of padding.

Try using concatenation or formatting instead:

print(f"      {myText}")

print("      " + myText)
3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.