1>>> name = "Eric"
2>>> age = 74
3>>> f"Hello, {name}. You are {age}."
4'Hello, Eric. You are 74.'
5
1date = "01/31/1956" # Guido Van Rossums birthday
2time = "9:30 AM"
3tags = ["high value", "high cost"]
4text = "Hello"
5
6# if you want to have it formatted standard but want the more appealing look
7return (
8 f'{date} - {time}\n'
9 f'Tags: {tags}\n'
10 f'Text: {text}'
11)
12
13# else if you want to have it formatted exactly as input
14return f'''{date} - {time},
15Tags: {tags},
16Text: {text}
17'''
1#python3.6 is required
2age = 12
3name = "Simon"
4print(f"Hi! My name is {name} and I am {age} years old")
1>>> name = "Fred"
2>>> f"He said his name is {name!r}."
3"He said his name is 'Fred'."
4>>> f"He said his name is {repr(name)}." # repr() is equivalent to !r
5"He said his name is 'Fred'."
6>>> width = 10
7>>> precision = 4
8>>> value = decimal.Decimal("12.34567")
9>>> f"result: {value:{width}.{precision}}" # nested fields
10'result: 12.35'
11>>> today = datetime(year=2017, month=1, day=27)
12>>> f"{today:%B %d, %Y}" # using date format specifier
13'January 27, 2017'
14>>> f"{today=:%B %d, %Y}" # using date format specifier and debugging
15'today=January 27, 2017'
16>>> number = 1024
17>>> f"{number:#0x}" # using integer format specifier
18'0x400'
19>>> foo = "bar"
20>>> f"{ foo = }" # preserves whitespace
21" foo = 'bar'"
22>>> line = "The mill's closed"
23>>> f"{line = }"
24'line = "The mill\'s closed"'
25>>> f"{line = :20}"
26"line = The mill's closed "
27>>> f"{line = !r:20}"
28'line = "The mill\'s closed" '