How to pad a string with spaces in Python?

Python strings have rjust(), ljust() and center() methods that allow strings to be padded with spaces.

s = "123".rjust(5)
assert s == "  123"
 
s = "123".ljust(5)
assert s == "123  "
 
s = "123".center(5)
assert s == " 123 "
Both comments and pings are currently closed.

Comments are closed.