From 3f2b6b18d7dfff6dabbf11ed4404adc3946b76d0 Mon Sep 17 00:00:00 2001 From: Tushar Khatri Date: Tue, 20 Sep 2022 23:15:25 +0530 Subject: [PATCH 1/2] Replace % with f-strings in multiple files mentioned in issue #19 --- advanced/functions.md | 4 ++-- advanced/magicmethods.md | 13 ++++--------- basics/answers.md | 10 +--------- basics/defining-functions.md | 2 +- basics/exceptions.md | 6 +++--- basics/larger-program.md | 6 +++--- basics/loops.md | 2 +- 7 files changed, 15 insertions(+), 28 deletions(-) diff --git a/advanced/functions.md b/advanced/functions.md index f1f176b..8f54e60 100644 --- a/advanced/functions.md +++ b/advanced/functions.md @@ -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: ") @@ -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: ") diff --git a/advanced/magicmethods.md b/advanced/magicmethods.md index a6bb5f5..aecfdaf 100644 --- a/advanced/magicmethods.md +++ b/advanced/magicmethods.md @@ -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 {message}") the message is 'hello' >>> ``` @@ -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={self.name}, founding_year={self.founding_year})' ... >>> github = Website('GitHub', 2008) >>> github @@ -174,8 +170,7 @@ follow one of these styles: ... self.name = name ... self.founding_year = founding_year ... def __repr__(self): - ... return '' % ( - ... self.name, self.founding_year) + ... return f'' ... >>> github = Website('GitHub', 2008) >>> github diff --git a/basics/answers.md b/basics/answers.md index b766cbf..d224b72 100644 --- a/basics/answers.md +++ b/basics/answers.md @@ -101,15 +101,7 @@ isn't exactly like mine but it works just fine it's ok, and you can 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: + recommend replacing the last line with this: ```python print(f"You entered {word1}, {word2}, {word3} and {word4}.") diff --git a/basics/defining-functions.md b/basics/defining-functions.md index 8d3aa97..9b78b94 100644 --- a/basics/defining-functions.md +++ b/basics/defining-functions.md @@ -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 diff --git a/basics/exceptions.md b/basics/exceptions.md index 25cc3f3..785fb18 100644 --- a/basics/exceptions.md +++ b/basics/exceptions.md @@ -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 @@ -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) diff --git a/basics/larger-program.md b/basics/larger-program.md index ef28393..4cd0cda 100644 --- a/basics/larger-program.md +++ b/basics/larger-program.md @@ -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) @@ -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) diff --git a/basics/loops.md b/basics/loops.md index 855f43d..7f693e1 100644 --- a/basics/loops.md +++ b/basics/loops.md @@ -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 :(") From 486a27eb1f08379dac698a01aa1e65b45c16ed04 Mon Sep 17 00:00:00 2001 From: Tushar Khatri Date: Thu, 22 Sep 2022 19:39:06 +0530 Subject: [PATCH 2/2] Add repr() in f-strings magicmethods.md --- advanced/magicmethods.md | 6 +++--- basics/answers.md | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/advanced/magicmethods.md b/advanced/magicmethods.md index aecfdaf..c333bbe 100644 --- a/advanced/magicmethods.md +++ b/advanced/magicmethods.md @@ -115,7 +115,7 @@ formatting](../basics/handy-stuff-strings.md#string-formatting) is also easy. ```python ->>> print(f"the message is {message}") +>>> print(f"the message is {repr(message)}") the message is 'hello' >>> ``` @@ -152,7 +152,7 @@ follow one of these styles: ... self.name = name ... self.founding_year = founding_year ... def __repr__(self): - ... return f'Website(name={self.name}, founding_year={self.founding_year})' + ... return f'Website(name={repr(self.name)}, founding_year={repr(self.founding_year)})' ... >>> github = Website('GitHub', 2008) >>> github @@ -170,7 +170,7 @@ follow one of these styles: ... self.name = name ... self.founding_year = founding_year ... def __repr__(self): - ... return f'' + ... return f'' ... >>> github = Website('GitHub', 2008) >>> github diff --git a/basics/answers.md b/basics/answers.md index d224b72..2e7f51a 100644 --- a/basics/answers.md +++ b/basics/answers.md @@ -100,8 +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 this: + The solution is string formatting. I recommend replacing the last line with this: ```python print(f"You entered {word1}, {word2}, {word3} and {word4}.")