You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: episodes/01-introduction.md
+60Lines changed: 60 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -189,6 +189,65 @@ else:
189
189
190
190
:::::::::::::::::::::::::::::::::::::::::::::::
191
191
192
+
::::::::::::::::::::::::::::::::::::::::: callout
193
+
194
+
## Formatting Variables and Objects in Strings
195
+
196
+
Throughout this course, we will be printing the values of variables inside strings to demonstrate what the code does. There are a number of ways to this in Python; in this course we will be using `f-strings` which are the recommended string interpolation syntax from version 3.6 onwards.
197
+
198
+
To use `f-strings` simply start a string literal with `f` or `F`, then embed whatever you want interpolating into the string within curly braces `{}`:
199
+
200
+
```python
201
+
two_int =2
202
+
two_str ="2"
203
+
f_string =f"{two_int} + {two_str} = {2+2}"
204
+
print(f_string)
205
+
```
206
+
```output
207
+
2 + 2 = 4
208
+
```
209
+
210
+
Numerical values can be formatted using `:` inside the curly braces. For example, an integer `i` can be made to have a width `n` using `{i:nd}`, or `{i:0nd}` to pad it out with zeros:
211
+
212
+
```python
213
+
print(f"One with four leading spaces {1:5d}")
214
+
print(f"One with four leading zeros {1:05d}")
215
+
```
216
+
```output
217
+
One with four leading spaces 1
218
+
One with four leading zeros 00001
219
+
```
220
+
221
+
Floating point variables can have their precision specified with a number after a decimal point and an `f`:
222
+
```python
223
+
pi =3.141592653589793
224
+
print(pi)
225
+
print(f"pi to three decimal places is {pi:.3f}")
226
+
```
227
+
```output
228
+
3.141592653589793
229
+
pi to three decimal places is 3.142
230
+
```
231
+
232
+
Or made to use scientific notation with an `e`:
233
+
234
+
```python
235
+
print(1e9)
236
+
print(f"A billion in scientific notation is {1e9:.1e}")
237
+
```
238
+
```output
239
+
1000000000.0
240
+
A billion in scientific notation is 1.0e+09
241
+
```
242
+
243
+
This is only scratching the surface of what you can do with `f-strings`, they are often the most powerful and concise way to format variables inside strings. However, there are occasions when other methods are preferable, and you should be careful using them with Python [before version 3.12](https://realpython.com/python-f-strings/#upgrading-f-strings-python-312-and-beyond).
244
+
245
+
To learn more, see: [Python f-strings](https://realpython.com/python-f-strings/).
We would like to reiterate the [Carpentries](https://carpentries.org/) stance on [Generative AI in coding](https://swcarpentry.github.io/python-novice-inflammation/01-intro.html#generative-ai) which you may have seen when preparing for this course.
@@ -207,6 +266,7 @@ To summarise the position, the recommendation is that you **avoid** getting help
0 commit comments