python fstring padding what it is and how to use it

[Python] F-String Padding: What It Is and How To Use It

What is F-String Padding in Python?

F-string padding in Python is a way to add characters to the beginning or end of a variable. This is useful for formatting output and it can be used to add spaces, dashes, or other characters to a string when you want to make it a certain length and make it more readable.

How to Use F-String Padding in Python

F-string padding is used by adding a colon, a symbol, an orientation and a number after the variable name:

– The colon is the syntax to tell Python that you want to add padding.
– The symbol is the character you want to add to the beginning or end of the string.
– The orientation is either a less than sign (<), a greater than sign (>), or a caret (^).
– The number is the length of the string you want to make.

Regarding the orientation, the less than sign (<) adds the padding to the end of the string, the greater than sign (>) adds the padding to the beginning of the string, and the caret (^) adds the padding to both sides of the string.

Here is an example of how to use f-string padding in Python:

name = "Alex"
print(f"{name:_<20}")
print(f"{name:->20}")
print(f"{name:*^20}")

>>> "Alex________________"
>>> "----------------Alex"
>>> "********Alex********"

As you can see:
– the first line adds 20 underscores to the end of the string
– the second line adds 20 dashes to the beginning of the string
– the third line adds 20 asterisks to both sides of the string, so that the variable is centered in the string

You don’t have to use underscores, dashes, or asterisks. You can use any character you want. Here is an example of how to use f-string padding in Python with a different character:

name = "Alex"
print(f"{name:#<20}")
print(f"{name:->20}")
print(f"{name:~^20}")

>>> "Alex################"
>>> "----------------Alex"
>>> "~~~~~~~~Alex~~~~~~~~"

You can even use the less than sign, greater than sign, or caret as the symbol:

name = "Alex"
print(f"{name:<<20}")
print(f"{name:>>20}")
print(f"{name:^^20}")

>>> "Alex<<<<<<<<<<<<<<<<"
>>> ">>>>>>>>>>>>>>>>Alex"
>>> "^^^^^^^^Alex^^^^^^^^"

The orientation is optional. But if you don’t specify an orientation, you won’t be able to choose a character to add to the string.

You just can choose the length of the string. Here is an example of how to use f-string padding in Python without an orientation:

name = "Alex"
print(f"{name:20}")

>>> "Alex                "

If you specify a character without an orientation, you will get an error:

name = "Alex"
print(f"{name:_20}")

>>> ValueError: Invalid format specifier.

However, you can specify only an orientation without a character. In this case, the default character is a space. Here is an example of how to use f-string padding in Python without a character:

name = "Alex"
print(f"{name:<20}")
print(f"{name:>20}")
print(f"{name:^20}")

>>> "Alex                "
>>> "                Alex"
>>> "        Alex        "

How to Use F-String Padding in Python 3.6 and Earlier

If you are using Python 3.6 or earlier, you can use the format() method to add padding to a string. The syntax is similar to the f-string syntax, but you use curly braces instead of an f at the beginning of the string.

Here is an example of how to use format() to add padding to a string:

name = "Alex"
print("{:_<20}".format(name))
print("{:->20}".format(name))
print("{:*^20}".format(name))
print("{:20}".format(name))

>>> "Alex________________"
>>> "----------------Alex"
>>> "********Alex********"
>>> "Alex                "

Again, you can’t use a character without an orientation. This will result in the same error (ValueError: Invalid format specifier).:

name = "Alex"
print("{:_20}".format(name))

>>> ValueError: Invalid format specifier.

If you are using Python 3.7 or later, you can still use the format() method, but it is easier to use f-strings instead.

What is the Difference Between F-String Padding and String Padding?

Essentially, f-string padding and string padding are the same thing. The only difference is that f-string padding is a new feature in Python 3.6 and later, and string padding is a feature that has been around since Python 2.0.