Skip to content

Replace % with f-strings in multiple files mentioned in issue #19 #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions advanced/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ For example, instead of this...

```python
def get_new_info(username):
print("Changing user information of %s." % username)
print(f"Changing user information of {username}.")
username = input("New username: ")
password = input("New password: ")
fullname = input("Full name: ")
Expand All @@ -66,7 +66,7 @@ class User:
# them here

def change_info(self):
print("Changing user information of %s." % self.username)
print(f"Changing user information of {self.username}.")
self.username = input("New username: ")
self.password = input("New password: ")
self.fullname = input("Full name: ")
Expand Down
13 changes: 4 additions & 9 deletions advanced/magicmethods.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,10 @@ the message is 'hello'

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

```python
>>> print("the message is %r" % (message,))
the message is 'hello'
>>> print("the message is {!r}".format(message))
>>> print(f"the message is {repr(message)}")
the message is 'hello'
>>>
```
Expand Down Expand Up @@ -155,8 +152,7 @@ follow one of these styles:
... self.name = name
... self.founding_year = founding_year
... def __repr__(self):
... return 'Website(name=%r, founding_year=%r)' % (
... self.name, self.founding_year)
... return f'Website(name={repr(self.name)}, founding_year={repr(self.founding_year)})'
...
>>> github = Website('GitHub', 2008)
>>> github
Expand All @@ -174,8 +170,7 @@ follow one of these styles:
... self.name = name
... self.founding_year = founding_year
... def __repr__(self):
... return '<Website %r, founded in %r>' % (
... self.name, self.founding_year)
... return f'<Website {repr(self.name)}, founded in {repr(self.founding_year)}>'
...
>>> github = Website('GitHub', 2008)
>>> github
Expand Down
11 changes: 1 addition & 10 deletions basics/answers.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,7 @@ isn't exactly like mine but it works just fine it's ok, and you can
just fine if we run it, but there's a problem. The last line is
really long and it's hard to see what it does.

The solution is string formatting. At the time of writing this, I
recommend replacing the last line with one of these:

```python
print("You entered %s, %s, %s and %s." % (word1, word2, word3, word4))
print("You entered {}, {}, {} and {}.".format(word1, word2, word3, word4))
```

In the future when most people will have Python 3.6 or newer, you
can also use this:
The solution is string formatting. I recommend replacing the last line with this:

```python
print(f"You entered {word1}, {word2}, {word3} and {word4}.")
Expand Down
2 changes: 1 addition & 1 deletion basics/defining-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ colors = ['red', 'yellow', 'blue', 'green', 'orange', 'pink', 'black',
'gray', 'white', 'brown']
choice = ask_until_correct("What's your favorite color?", colors,
error_message="I don't know that color.")
print("Your favorite color is %s!" % choice)
print(f"Your favorite color is {choice}!")
```

## Summary
Expand Down
6 changes: 3 additions & 3 deletions basics/exceptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ text = input("Enter a number: ")
try:
number = int(text)
except ValueError:
print("'%s' is not a number." % text, file=sys.stderr)
print(f"'{text}' is not a number.", file=sys.stderr)
sys.exit(1)
print("Your number doubled is %d." % (number * 2))
print(f"Your number doubled is {(number * 2)}.")
```

## Raising exceptions
Expand Down Expand Up @@ -452,7 +452,7 @@ def greet():
try:
greet()
except OSError:
print("Cannot read '%s'!" % filename, file=sys.stderr)
print(f"Cannot read '{filename}'!", file=sys.stderr)
if askyesno("Would you like to create a default greeting file?"):
with open(filename, 'w') as f:
print(default_greeting, file=f)
Expand Down
6 changes: 3 additions & 3 deletions basics/larger-program.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def ask_questions(answers):
print("Correct!")
correct.append(question)
else:
print("Wrong! The correct answer is %s." % answer)
print(f"Wrong! The correct answer is {answer}.")
wrong.append(question)

return (correct, wrong)
Expand Down Expand Up @@ -181,11 +181,11 @@ def ask_questions(answers):
wrong = []

for question, answer in answers.items():
if input('%s = ' % question).strip() == answer:
if input(f'{question} = ').strip() == answer:
print("Correct!")
correct.append(question)
else:
print("Wrong! The correct answer is %s." % answer)
print(f"Wrong! The correct answer is {answer}.")
wrong.append(question)

return (correct, wrong)
Expand Down
2 changes: 1 addition & 1 deletion basics/loops.md
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ while True:
print("I don't know anybody yet.")
else:
for name in namelist:
print("I know %s!" % name)
print(f"I know {name}!")

else:
print("I don't understand :(")
Expand Down