Skip to content

Commit 0995381

Browse files
authored
Merge pull request #409 from realpython/python312-preview-f-string
Sample code - Python 3.12 preview f-strings
2 parents 698f83a + d06886d commit 0995381

5 files changed

Lines changed: 54 additions & 0 deletions

File tree

python-312/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ You can learn more about Python 3.12's new features in the following Real Python
1414

1515
- [Python 3.12 Preview: Ever Better Error Messages](https://realpython.com/python312-error-messages/)
1616
- [Python 3.12 Preview: Support For the Linux `perf` Profiler](https://realpython.com/python312-perf-profiler/)
17+
- [Python 3.12 Preview: More Intuitive and Consistent F-Strings](https://realpython.com/python312-f-strings/)
1718

1819
You'll find examples from all these tutorials in this repository.
1920

@@ -117,6 +118,24 @@ $ cat traces_censored.txt | flamegraph.pl --minwidth 10 > flamegraph.svg
117118

118119
See [Support For the Linux `perf` Profiler in Python 3.12](https://realpython.com/python312-perf-profiler/) for more information.
119120

121+
### New F-String Implementation and Syntax Formalization
122+
123+
Once you have 3.12 installed, open any file from the `f-string/` folder. Uncomment the code as needed. [Run the scripts](https://realpython.com/run-python-scripts/) from your command line or execute the code directly in a [REPL](https://realpython.com/python-repl/) session. You'll see the new f-string implementation in action.
124+
125+
For example, go ahead and run the following command:
126+
127+
```console
128+
$ python backslashes.py
129+
Hello
130+
World!
131+
I
132+
am
133+
a
134+
Pythonista!
135+
```
136+
137+
In this example, you can see how the new implementation of f-strings allows you to include backslashes in embedded expressions. This wasn't possible with f-strings in earlier versions of Python.
138+
120139
## Authors
121140

122141
- **Martin Breuss**, E-mail: [martin@realpython.com](martin@realpython.com)

python-312/f-string/backslashes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
words = ["Hello", "World!", "I", "am", "a", "Pythonista!"]
2+
3+
# Uncomment the following line and run it in Python 3.12
4+
# print(f"{'\n'.join(words)}")

python-312/f-string/comments.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
employee = {
2+
"name": "John Doe",
3+
"age": 35,
4+
"job": "Python Developer",
5+
}
6+
7+
# Uncomment the following code and run it in Python 3.12
8+
# f"""Storing employee's data: {
9+
# employee['name'].upper() # Always uppercase name before storing
10+
# }"""

python-312/f-string/nested.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Uncomment the following code and run it in Python 3.12
2+
3+
# f"{
4+
# f"{
5+
# f"{
6+
# f"{
7+
# f"{
8+
# f"Deeply nested f-string!"
9+
# }"
10+
# }"
11+
# }"
12+
# }"
13+
# }"

python-312/f-string/quotes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
employee = {
2+
"name": "John Doe",
3+
"age": 35,
4+
"job": "Python Developer",
5+
}
6+
7+
# Uncomment the following line and run it in Python 3.12
8+
# f"Employee: {employee["name"]}"

0 commit comments

Comments
 (0)