1# f-strings are short for formatted string like the following
2# you can use the formatted string by two diffrent ways
3# 1
4name = "John Smith"
5print(f"Hello, {name}") # output = Hello, John Smith
6
7# 2
8name = "John Smith"
9print("Hello, {}".format(name)) # output = Hello, John Smith
10
1# If you want to use repr in f-string use "!r"
2# Normal behavior (using str)
3>>> color = "blue\ngreen"
4>>> day = datetime.date(2020, 6, 4)
5>>> f"Color is {color} and day is {day}"
6'Color is blue\ngreen and day is 2020-06-04'
7# Alternate behavior (using repr)
8>>> f"Color is {color!r} and day is {day!r}"
9"Color is 'blue\\ngreen' and day is datetime.date(2020, 6, 4)"
1"""
2
3An f-string stands for 'function-string' it's just used to work with
4strings more appropiately, they do the exact same job as concantenating
5strings but are more efficient and readable.
6
7"""
8# Concantenating strings:
9
10Age = "25"
11
12print("I am "+Age+" years old.")
13
14# Using f strings:
15
16Age = 25
17
18print(f"I am {Age} years old.")
19
20# ^ notice the letter 'f' at the begining of the string.
21# That defines the string as being an f-string.
22
23# A third way of inputting variables into a string is by using
24# .format()
25
26Age = "25"
27
28print("I am {} years old.".format(Age))
29
30# If you had more than one variable:
31
32Age = "25"
33Name = "Jeff"
34
35print("I am {} years old, and my name is {}.".format(Age,Name))
1num_01, num_02, num_03 = 1, 2, 3
2print(f"Numbers : {num_01}, {num_02}, {num_03}")
3
4"""
5>>> Numbers: 1, 2, 3
6"""
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" '