Skip to content

Commit 5183b3d

Browse files
Replace % with f-strings in multiple files mentioned in issue #19 (#41)
1 parent 78504ac commit 5183b3d

File tree

7 files changed

+15
-29
lines changed

7 files changed

+15
-29
lines changed

advanced/functions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ For example, instead of this...
5050

5151
```python
5252
def get_new_info(username):
53-
print("Changing user information of %s." % username)
53+
print(f"Changing user information of {username}.")
5454
username = input("New username: ")
5555
password = input("New password: ")
5656
fullname = input("Full name: ")
@@ -66,7 +66,7 @@ class User:
6666
# them here
6767

6868
def change_info(self):
69-
print("Changing user information of %s." % self.username)
69+
print(f"Changing user information of {self.username}.")
7070
self.username = input("New username: ")
7171
self.password = input("New password: ")
7272
self.fullname = input("Full name: ")

advanced/magicmethods.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,10 @@ the message is 'hello'
112112

113113
Combining `repr()` with [string
114114
formatting](../basics/handy-stuff-strings.md#string-formatting) is also
115-
easy. `%` formatting has a `%r` formatter, and `.format()` formatting
116-
has a `!r` flag.
115+
easy.
117116

118117
```python
119-
>>> print("the message is %r" % (message,))
120-
the message is 'hello'
121-
>>> print("the message is {!r}".format(message))
118+
>>> print(f"the message is {repr(message)}")
122119
the message is 'hello'
123120
>>>
124121
```
@@ -155,8 +152,7 @@ follow one of these styles:
155152
... self.name = name
156153
... self.founding_year = founding_year
157154
... def __repr__(self):
158-
... return 'Website(name=%r, founding_year=%r)' % (
159-
... self.name, self.founding_year)
155+
... return f'Website(name={repr(self.name)}, founding_year={repr(self.founding_year)})'
160156
...
161157
>>> github = Website('GitHub', 2008)
162158
>>> github
@@ -174,8 +170,7 @@ follow one of these styles:
174170
... self.name = name
175171
... self.founding_year = founding_year
176172
... def __repr__(self):
177-
... return '<Website %r, founded in %r>' % (
178-
... self.name, self.founding_year)
173+
... return f'<Website {repr(self.name)}, founded in {repr(self.founding_year)}>'
179174
...
180175
>>> github = Website('GitHub', 2008)
181176
>>> github

basics/answers.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,7 @@ isn't exactly like mine but it works just fine it's ok, and you can
100100
just fine if we run it, but there's a problem. The last line is
101101
really long and it's hard to see what it does.
102102

103-
The solution is string formatting. At the time of writing this, I
104-
recommend replacing the last line with one of these:
105-
106-
```python
107-
print("You entered %s, %s, %s and %s." % (word1, word2, word3, word4))
108-
print("You entered {}, {}, {} and {}.".format(word1, word2, word3, word4))
109-
```
110-
111-
In the future when most people will have Python 3.6 or newer, you
112-
can also use this:
103+
The solution is string formatting. I recommend replacing the last line with this:
113104

114105
```python
115106
print(f"You entered {word1}, {word2}, {word3} and {word4}.")

basics/defining-functions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ colors = ['red', 'yellow', 'blue', 'green', 'orange', 'pink', 'black',
510510
'gray', 'white', 'brown']
511511
choice = ask_until_correct("What's your favorite color?", colors,
512512
error_message="I don't know that color.")
513-
print("Your favorite color is %s!" % choice)
513+
print(f"Your favorite color is {choice}!")
514514
```
515515

516516
## Summary

basics/exceptions.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,9 @@ text = input("Enter a number: ")
249249
try:
250250
number = int(text)
251251
except ValueError:
252-
print("'%s' is not a number." % text, file=sys.stderr)
252+
print(f"'{text}' is not a number.", file=sys.stderr)
253253
sys.exit(1)
254-
print("Your number doubled is %d." % (number * 2))
254+
print(f"Your number doubled is {(number * 2)}.")
255255
```
256256

257257
## Raising exceptions
@@ -452,7 +452,7 @@ def greet():
452452
try:
453453
greet()
454454
except OSError:
455-
print("Cannot read '%s'!" % filename, file=sys.stderr)
455+
print(f"Cannot read '{filename}'!", file=sys.stderr)
456456
if askyesno("Would you like to create a default greeting file?"):
457457
with open(filename, 'w') as f:
458458
print(default_greeting, file=f)

basics/larger-program.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def ask_questions(answers):
8282
print("Correct!")
8383
correct.append(question)
8484
else:
85-
print("Wrong! The correct answer is %s." % answer)
85+
print(f"Wrong! The correct answer is {answer}.")
8686
wrong.append(question)
8787

8888
return (correct, wrong)
@@ -181,11 +181,11 @@ def ask_questions(answers):
181181
wrong = []
182182

183183
for question, answer in answers.items():
184-
if input('%s = ' % question).strip() == answer:
184+
if input(f'{question} = ').strip() == answer:
185185
print("Correct!")
186186
correct.append(question)
187187
else:
188-
print("Wrong! The correct answer is %s." % answer)
188+
print(f"Wrong! The correct answer is {answer}.")
189189
wrong.append(question)
190190

191191
return (correct, wrong)

basics/loops.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ while True:
416416
print("I don't know anybody yet.")
417417
else:
418418
for name in namelist:
419-
print("I know %s!" % name)
419+
print(f"I know {name}!")
420420

421421
else:
422422
print("I don't understand :(")

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy