Skip to content

Commit 2c9d38c

Browse files
author
Scott Archer-Nicholls
committed
Revert "reverted keypoints changes to avoid clash with #77"
This reverts commit ab11718.
1 parent ab11718 commit 2c9d38c

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

episodes/01-introduction.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,65 @@ else:
189189

190190
:::::::::::::::::::::::::::::::::::::::::::::::
191191

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/).
246+
247+
::::::::::::::::::::::::::::::::::::::::::::::::::
248+
249+
::::::::::::::::::::::::::::::::::::::::: callout
250+
192251
## Generative AI
193252

194253
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
207266
- loops
208267
- conditionals
209268
- string formatting
269+
- avoid using generative AI during this course
210270

211271
::::::::::::::::::::::::::::::::::::::::::::::::::
212272

0 commit comments

Comments
 (0)