Example 1: f string repr
>>> color = "blue\ngreen"
>>> day = datetime.date(2020, 6, 4)
>>> f"Color is {color} and day is {day}"
'Color is blue\ngreen and day is 2020-06-04'
>>> f"Color is {color!r} and day is {day!r}"
"Color is 'blue\\ngreen' and day is datetime.date(2020, 6, 4)"
Example 2: python f string
>>> name = "Eric"
>>> age = 74
>>> f"Hello, {name}. You are {age}."
'Hello, Eric. You are 74.'
Example 3: python f string literal
name = "George"
age = 16
favorite_food = "pizza"
print("My name is", name, ", my age is", age, ", and my favorite food is", favorite_food)
print("My name is "+ name +", my age is "+ str(age)+ ", and my favorite food is "+ favorite_food)
print(f"My name is {name}, my age is {age}, and my favorite food is {favorite_food}")
"""
Why put the f before the string, you ask?
Well if you didnt, the output would literally be {name} instead of the actual variable
One more thing: this is fairly new and only works with python 3.6 and higher.
"""
Example 4: format specificer f strings python
val = 12.3
print(f'{val:.2f}')
print(f'{val:.5f}')
Example 5: python f string
import math
print('[[fill]align] | f\'{"text":10}\' | ', f'{"text":10}')
print('[[fill]align] | f\'{"text":>10}\' | ', f'{"text":>10}')
print('[[fill]align] | f\'{"text":^10}\' | ', f'{"text":^10}')
print('[[fill]align] | f\'{"text":#>10}\' | ', f'{"text":#>10}')
print('[[fill]align] | f\'{"text":#<10}\' | ', f'{"text":#<10}')
print('[[fill]align] | f\'{"text":#^10}\' | ', f'{"text":#^10}')
print('[[fill]align] with numbers | f\'{12345:0=10}\' | ', f'{12345:0=10}')
print('[[fill]align] with numbers | f\'{-12345:0=10}\' | ', f'{-12345:0=10}')
print('[[fill]align] with numbers | f\'{12345:010}\' | ', f'{12345:010}')
print('[[fill]align] with numbers | f\'{-12345:010}\' | ', f'{-12345:010}')
print('[.precision] | f\'{math.pi:.2f}\' | ', f'{math.pi:.2f}')
print('[grouping_option] | f\'{1000000:,.2f}\' | ', f'{1000000:,.2f}')
print('[grouping_option] | f\'{1000000:_.2f}\' | ', f'{1000000:_.2f}')
print('[sign] (+/-) | f\'{12345:+}\' | ', f'{12345:+}')
print('[sign] (+/-) | f\'{-12345:+}\' | ', f'{-12345:+}')
print('[sign] (+/-) | f\'{12345:+10}\' | ', f'{12345:+10}')
print('[sign] (+/-) | f\'{-12345:+10}\' | ', f'{-12345:+10}')
print('[sign] (+/-) | f\'{12345:+010}\' | ', f'{12345:+010}')
print('[sign] (+/-) | f\'{-12345:+010}\' | ', f'{-12345:+010}')
print('[b] (binary) | f\'{10:b}\' | ', f'{10:b}')
print('[o] (octal) | f\'{10:o}\' | ', f'{10:o}')
print('[x] (hexadecimal) | f\'{100:x}\' | ', f'{100:x}')
print('[#b] (with notation base) | f\'{10:#b}\' | ', f'{10:#b}')
print('[#0] (with notation base) | f\'{10:#o}\' | ', f'{10:#o}')
print('[#x] (with notation base) | f\'{10:#x}\' | ', f'{10:#x}')
print('[e] (scientific notation) | f\'{345600000000:e}\' | ', f'{345600000000:e}')
print('[c] (character type) | f\'{65:c}\' | ', f'{65:c}')
print('percentage (multiply by 100) | f\'{0.25:0%}\' | ', f'{0.25:0%}')
print('percentage (multiply by 100) | f\'{0.25:.0%}\' | ', f'{0.25:.0%}')