-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Sample code for the article on f-strings #435
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e23a5fc
Sample code for the article on f-strings
lpozo 4f0099c
Fix linter issues
lpozo 28926ce
Merge branch 'master' into python-f-string
martin-martin 5822b80
Merge branch 'master' into python-f-string
lpozo b7ffcc5
TR updates
lpozo 89a7851
Update print text
martin-martin 7443abd
TR updates, second round
lpozo 096c371
Merge remote-tracking branch 'origin/python-f-string' into python-f-s…
lpozo 4404159
Merge branch 'master' into python-f-string
KateFinegan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Python's F-String: An Improved String Interpolation and Formatting Tool | ||
|
||
This folder provides the code examples for the Real Python tutorial [Python's F-String: An Improved String Interpolation and Formatting Tool](https://realpython.com/python-f-string-update/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
name = "Jane" | ||
age = 25 | ||
f"Hello, {name}! You're {age} years old." | ||
|
||
f"{2 * 21}" | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
name = "Jane" | ||
age = 25 | ||
|
||
"Hello, {}! You're {} years old.".format(name, age) | ||
"Hello, {name}! You're {age} years old.".format(name="Jane", age=25) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
name = "Jane" | ||
age = 25 | ||
|
||
"Hello, %s!" % name | ||
"Hello, %s! You're %s years old." % (name, age) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
person = {"name": "Jane Doe", "age": 25} | ||
f"Hello, {person['name']}! You're {person['age']} years old." | ||
|
||
"Hello, {name}! You're {age} years old.".format(**person) | ||
"Hello, {name}!".format(**person) | ||
|
||
"Hello, %(name)s! You're %(age)s years old." % person | ||
"Hello, %(name)s!" % person |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
variable = "Some mysterious value" | ||
print(f"{variable = }") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
name = "Jane" | ||
age = 25 | ||
|
||
f"Hello, {name.upper()}! You're {age} years old." | ||
f"{[2**n for n in range(3, 9)]}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from person import Person | ||
|
||
jane = Person("Jane Doe", 25) | ||
|
||
f"{jane!s}" | ||
f"{jane!r}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
balance = 5425.9292 | ||
f"Balance: ${balance:.2f}" | ||
|
||
heading = "Centered string" | ||
f"{heading:=^30}" | ||
|
||
integer = -1234567 | ||
f"Comma as thousand separators: {integer:,}" | ||
sep = "_" | ||
f"User's thousand separators: {integer:{sep}}" | ||
|
||
floating_point = 1234567.9876 | ||
f"Comma as thousand separator: {floating_point:,.2f}" | ||
|
||
date = (9, 6, 2023) | ||
f"Date: {date[0]:02}-{date[1]:02}-{date[2]}" | ||
date = (9, 26, 2023) | ||
f"Date: {date[0]:02}/{date[1]:02}/{date[2]}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
"Balance: ${:.2f}".format(5425.9292) | ||
"{:=^30}".format("Centered string") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
"Balance: $%.2f" % 5425.9292 | ||
print("Name: %s\nAge: %5s" % ("John", 35)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
greeting_template = "{greeting} Pythonista!" | ||
|
||
greeting_en = "Good Morning!" | ||
greeting_es = "¡Buenos días!" | ||
greeting_fr = "Bonjour!" | ||
|
||
for greeting in (greeting_en, greeting_es, greeting_fr): | ||
print(greeting_template.format(greeting=greeting)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import logging | ||
|
||
msg_template = "Hey %s! You're using logging!" | ||
logging.warning(msg_template, "Pythonista") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import timeit | ||
|
||
name = "Linda Smith" | ||
age = 40 | ||
strings = { | ||
"Modulo operator": "'Name: %s Age: %s' % (name, age)", | ||
".format() method": "'Name: {} Age: {}'.format(name, age)", | ||
"f_string": "f'Name: {name} Age: {age}'", | ||
} | ||
|
||
|
||
def run_performance_test(strings): | ||
max_length = len(max(strings, key=len)) | ||
for tool, string in strings.items(): | ||
time = timeit.timeit(string, number=1000000, globals=globals()) * 1000 | ||
print(f"{tool}: {time:>{max_length - len(tool) + 6}.2f} ms") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Person: | ||
def __init__(self, name, age): | ||
self.name = name | ||
self.age = age | ||
|
||
def __str__(self): | ||
return f"I'm {self.name}, and I'm {self.age} years old." | ||
|
||
def __repr__(self): | ||
return f"{type(self).__name__}(name='{self.name}', age={self.age})" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
name = "Jane" | ||
age = 25 | ||
|
||
print("Hello, %s! You're %s years old." % (name, age)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
name = "Jane" | ||
age = 25 | ||
|
||
print(f"Hello, {name}! You're {age} years old.") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that for providing these code blocks as files it makes sense to wrap REPL session output into calls to
print()
. That's at least what I've been doing, to make it convenient for readers to run.Do we have a guideline on doing/not doing that @gahjelle ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess that wrapping the code in calls to
print()
can confuse the reader because the code differs from the code in the article.