Format string to exact length

From the ‘’ py4e assignment:
This formatting ensures that ‘description’ is always a max of 23 characters long, and in a row that is always 23 characters long:

print(f"{description[:23]:23} {amount:7.2f}")

It works, but seems to me to be a clumsy looking way of achieving the goal.
Is there a better way to specify that the string will always be exactly 23 characters long?

Why clumsy? It looks like an excellent and concise way of achieving this.

1 Like

Thanks, glad you like it! I was looking for a single expression that would force a string to be a specified length, rather than having to use 2 expressions to do that. It just seemed like a thing that should exist! I guess if it doesn’t, I’m not missing anything.

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