Hurry! The input string needs to be of a certain length. Python has plenty of built-in padding mechanisms, but in a rush, parsing the string formatting documentation feels slow.

Here's the cheat sheet:

Left pad with zeroes

"12345".zfill(10)   # "0000012345"
"-12345".zfill(10)  # "-000012345"

Available for use with non-numeric strings, but beware of leading hyphens:

"short_message".zfill(20)   # '0000000short_message"
"-short_message".zfill(20)  # '-000000short_message"

When leading hyphens shouldn't be interpreted as a negative sign:

"{:0>10}".format("12345")           # "0000012345"
"{:0>10}".format("-12345")          # "0000-12345"

"{:0>20}".format("short_message")   # "0000000short_message"
"{:0>20}".format("-short_message")  # "000000-short_message"

Alternative to zfill() when working directly with integers/floats instead of strings:

"{:010}".format(12345)   # "0000012345"
"{:010}".format(-12345)  # "-000012345"

The same alternative, but with dynamic message lengths:

"{num:0{n}}".format(num=12345, n=10)  # "0000012345"

Dynamic message lengths, the Python3 way:

num = 12345
n = 10
f"{num:0{n}}"  # "0000012345"

Left pad with spaces

"short_message".rjust(20)         # "       short_message"
"{:>20}".format("short_message")  # "       short_message"

Dynamic message lengths:

"{msg:>{n}}".format(msg="short_message", n=20)  # "       short_message"

Drop the > alignment when working directly with integers/floats instead of strings:

"{:10}".format(12345)                # "     12345"
"{:10}".format(-12345)               # "    -12345"

"{num:{n}}".format(num=12345, n=10)  # "     12345"

Left pad with any character

"short_message".rjust(20, "x")     # "xxxxxxxshort_message"
"{:x>20}".format("short_message")  # "xxxxxxxshort_message"

Dynamic message lengths:

"{msg:x>{n}}".format(msg="short_message", n=20)  # "xxxxxxxshort_message"

Right pad with any character

"short_message".ljust(20, "x")     # "short_messagexxxxxxx"
"{:x<20}".format("short_message")  # "short_messagexxxxxxx"

Center with any character

"short_message".center(20, "x")    # "xxxshort_messagexxxx"
"{:x^20}".format("short_message")  # "xxxshort_messagexxxx"