F-strings are a powerful and easy-to-use way to format strings in Python. They are more readable than traditional string formatting methods and offer more flexibility. In this blog post, we will explore five useful F-string formatting tricks that you can use to make your code more readable and maintainable.

Use f-string to format number:

You can insert any character after the colon in an f-string to add them as thousand separators. For example, the following code will format the number 1234567 as 1,234,567:

number = 1234567
print(f"The number is: {number:,}")
print(f"The number is: {number:_}")

=== Result ===
The number is: 1,234,567
The number is: 1_234_567

Align text:

You can use the right arrow > to right-align text, the left arrow < for left-align, and the up arrow ^ to center-align text. You can also specify a fill character to fill the empty spaces. For example, the following code will right-align the text “Hello” with 20 spaces:

text = "Hello"
print(f"{text:>20}")
print(f"{text:<20}")
print(f"{text:^20}")

=== Result ===
               Hello
Hello               
       Hello        

You can also specify the character that you want to use to fill the empty space.

text = "Hello"
print(f"{text:+>20}")
print(f"{text:-<20}")
print(f"{text:=^20}")

=== Result ===
+++++++++++++++Hello
Hello---------------
=======Hello========

Format date and time:

You can use date time format specifiers like %d, %m, and %Y to format date and time in an f-string. You can also use %I, %p, %H, %M, and %S to format the time in 12-hour format, 24-hour format, etc. For example, the following code will format the current date and time to %Y-%m-%d %H:%M:%S:

from datetime import datetime

now = datetime.now()
print(f"The current date and time is: {now:%Y-%m-%d %H:%M:%S}")

=== Result ===
The current date and time is: 2024-02-13 19:24:06

You can check this site to find other date time format specifiers.

Round numbers:

You can use the f specifier followed by the number of decimal places to round a number to that many decimal places. For example, :.2f rounds a number to two decimal places. The following code will round the number 3.14159 to two decimal places:

number = 3.14159
print(f"The number rounded to two decimal places is: {number:.2f}")


=== Result ===
The number rounded to two decimal places is: 3.14

Debug code using f-strings:

You can add an equal sign and an expression inside the curly braces of an f-string to evaluate the expression and print the result. This can be useful for debugging code because it shows you the value of the expression at that point in the code. For example, the following code will print the value of the variable x:

x = 10
print(f"The value of x is: {x=}")

=== Result ===
The value of x is: x=10

I hope these five tips help you write more readable and maintainable Python code!

In addition to the tips in the post, here are some other things to keep in mind when using F-strings:

  • F-strings can be used to format any type of data, not just strings.
  • You can use f-strings inside other f-strings.
  • You can use f-strings to create multi-line strings.

I hope this blog post has been helpful! Please let me know if you have any questions.