-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathf-string.py
More file actions
175 lines (138 loc) · 5.16 KB
/
f-string.py
File metadata and controls
175 lines (138 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
'''
This Python file created by following Real Python tutorial "Python 3's f-Strings: An Improved String Formatting
Syntax (Guide)" by Joanna Jablonski.
Joanna's tutorial can be found here: https://realpython.com/python-f-strings/
Joanna's GitHub can be found here: https://github.com/jablonskidev
'''
#Ways of embedding Python expressions inside string literals for formatting: %-formatting, str.format(), f-strings.
#Option 1: %-formatting (easy to implement, but even Python docs admit there are quirks; it should be avoided)
#Substituting a name:
name = "Eric"
"Hello, %s." % name
#Must use a tuple for multiple variables:
name = "Eric"
age = 74
"Hello, %s. You are %s." % (name, age)
#Problem: Mo Variables == Mo Complexity:
first_name = "Eric"
last_name = "Idle"
age = 74
profession = "comedian"
affiliation = "Monty Python"
"Hello, %s %s. You are %s. You are a %s. You were a member of %s." % (first_name, last_name, age, profession, affiliation)
#Option 2: str.format() (new and improved, uses normal function call syntax)
#Now the replacement fields are marked by curly braces:
"Hello, {}. You are {}.".format(name, age)
#Variables can be referenced by their index instead of order in the tuple:
"Hello, {1}. You are {0}.".format(age, name)
#Can also be referenced by variable names:
person = {'name': 'Eric', 'age': 74}
"Hello, {name}. You are {age}.".format(name=person['name'], age = person['age'])
#Can also use **dict_name to condense the above example:
person = {'name':'Eric', 'age': 74}
"Hello, {name}. You are {age}.".format(**person)
#Conclusion: str.format() is easier to read! BUT it can be quite long-winded w/ multiple paramters + longer strings...
first_name = 'Eric'
last_name = 'Idle'
age = 74
profession = 'comedian'
affiliation = 'Monty Python'
print(("Hello, {first_name} {last_name}. You are {age}. You are a {profession}." +
"You were a member of {affiliation}.").format(first_name=first_name, last_name=last_name, age=age, \
profession=profession, affiliation=affiliation))
#f-Strings joined in Python 3.6, info @ PEP 498 by Eric V. Smith, AKA formatted string literals
#The examples above could be expressed as:
name = "Eric"
age = 74
f"Hello, {name}. You are {age}."
#or
F"Hello, {name}. You are {age}."
#f-strings are evaluated at runtime, can use expressions, call functions or methods, and use objects
#Expression:
f"{2*37}"
#Call function:
def to_lowercase(input):
return input.lower()
name = "Eric Idle"
f"{to_lowercase(name)} is funny."
#Call method:
f"{name.lower()} is funny."
#Use an object created from classes f-strings:
class Comedian:
def __init__(self, first_name, last_name, age):
self.first_name = first_name
self.last_name = last_name
self.age = age
def __str__(self):
return f"{self.first_name} {self.last_name} is {self.age}."
def __repr__(self):
return f"{self.first_name} {self.last_name} is {self.age}. Surprise!!"
new_comedian = Comedian("Eric", "Idle", "74")
f"{new_comedian}"
#3 types of conversion flags: !s which calls str() on the value, !r calls repr(), !a calls ascii().
#f-strings use __str__() by default, __repr()__ can be called with its conversion flag.
f"{new_comedian}"
f"{new_comedian!r}"
#Multiline f-Strings are supported
name = "Eric"
profession = "comedian"
affiliation = "Monty Python"
message = (
f"Hi {name}. "
f"You are a {profession}. "
f"You were in {affiliation}."
)
message
#the following code wouldn't work:
message = (
f"Hi {name}. "
"You are a {profession}. "
"You were in {affiliation}."
)
message
#Can also escape a return w/ the backslash:
message = f"Hi {name}. " \
f"You are a {profession}. " \
f"You were in {affiliation}."
message
#Messy example with triple quotes:
message = f"""
Hi {name}.
You are a {profession}.
You were in {affiliation}.
"""
message
#f-strings are fast. Again, they are executed at runtime
#Speed comparison of %-formatting, str.format(), & f-Strings:
import timeit
timeit.timeit('''name="Eric"
age = 74
'%s is %s.' % (name, age)''', number=10000)
import timeit
timeit.timeit('''name="Eric"
age=74
'{} is {}.'.format(name,age)''', number=10000)
import timeit
timeit.timeit("""name="Eric"
age=74
f'{name} is {age}.'""", number=10000)
#f-Strings are the fastest @ runtime
#Need a keen eye for detail, as shown here:
#these will work:
f"{'Eric Idle'}"
f'{"Eric Idle"}'
f"""Eric Idle"""
f'''Eric Idle'''
#cannot use same type of parentheses outside of curly braces as inside
#can use an escape to weasel around this rule
f"The \"comedian\" is {name}, aged {age}."
#when using single quotes dictionary keys, use doeble quotes for the f-strings containing the keys
comedian = {'name': 'Eric Idle', 'age':74}
f"The comedian is {comedian['name']}, aged {comedian['age']}."
#Can get braces to appear if using
f'{{70+4}}'
f'{{{70+4}}}'
f'{{{{70+4}}}}'
f'{{{{{70+4}}}}}'
#this returns '{70+4}','{74}','{{70+4}}','{{74}}'
#Cannot use backslashes or comments inside of the expression part of an f-string