Skip to content

Commit 22725a4

Browse files
Replace older formatting methods with f-strings (Akuli#39)
1 parent ebd82e1 commit 22725a4

File tree

2 files changed

+6
-71
lines changed

2 files changed

+6
-71
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ or very little programming experience. If you have programmed a lot in
1212
the past using some other language you may want to read [the official
1313
tutorial](https://docs.python.org/3/tutorial/) instead.
1414

15-
You can use Python 3.5 or any newer Python with this tutorial. **Don't
15+
You can use Python 3.6 or any newer Python with this tutorial. **Don't
1616
use Python 2 because it's no longer supported.**
1717

1818
## List of contents

basics/handy-stuff-strings.md

Lines changed: 5 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -241,82 +241,17 @@ Instead it's recommended to use string formatting. It means putting
241241
other things in the middle of a string.
242242

243243
Python has multiple ways to format strings. One is not necessarily
244-
better than others, they are just different. Here's a few ways to solve
245-
our problem:
244+
better than others; they each have their own advantages and disadvantages.
245+
In this tutorial, we will focus on f-strings, which is the most common and usually the easiest way.
246246

247-
- `.format()`-formatting, also known as new-style formatting. This
248-
formatting style has a lot of features, but it's a little bit more
249-
typing than `%s`-formatting.
250-
251-
```python
252-
>>> "Hello {}.".format(name)
253-
'Hello Akuli.'
254-
>>> "My name is {} and I'm on the {} channel on {}.".format(name, channel, network)
255-
"My name is Akuli and I'm on the ##learnpython channel on freenode."
256-
>>>
257-
```
258-
259-
- `%s`-formatting, also known as old-style formatting. This has less
260-
features than `.format()`-formatting, but `'Hello %s.' % name` is
261-
shorter and faster to type than `'Hello {}.'.format(name)`. I like
262-
to use `%s` formatting for simple things and `.format` when I need
263-
more powerful features.
264-
265-
```python
266-
>>> "Hello %s." % name
267-
'Hello Akuli.'
268-
>>> "My name is %s and I'm on the %s channel on %s." % (name, channel, network)
269-
"My name is Akuli and I'm on the ##learnpython channel on freenode."
270-
>>>
271-
```
272-
273-
In the second example we had `(name, channel, network)` on the right
274-
side of the `%` sign. It was a tuple, and we'll talk more about them
275-
[later](lists-and-tuples.md#tuples).
276-
277-
If we have a variable that may be a tuple we need to wrap it in another
278-
tuple when formatting:
279-
280-
```python
281-
>>> thestuff = (1, 2, 3)
282-
>>> "we have %s" % thestuff
283-
Traceback (most recent call last):
284-
File "<stdin>", line 1, in <module>
285-
TypeError: not all arguments converted during string formatting
286-
>>> "we have %s and %s" % ("hello", thestuff)
287-
'we have hello and (1, 2, 3)'
288-
>>> "we have %s" % (thestuff,)
289-
'we have (1, 2, 3)'
290-
>>>
291-
```
292-
293-
Here `(thestuff,)` was a tuple that contained nothing but `thestuff`.
294-
295-
- f-strings are even less typing, but new in Python 3.6. **Use this only if
296-
you know that nobody will need to run your code on Python versions older
297-
than 3.6.** Here the f is short for "format", and the content of the
298-
string is same as it would be with `.format()` but we can use variables
299-
directly.
300-
301-
```python
302-
>>> f"My name is {name} and I'm on the {channel} channel on {network}."
303-
"My name is Akuli and I'm on the ##learnpython channel on freenode."
304-
>>>
305-
```
306-
307-
All of these formatting styles have many other features also:
247+
`f` in f-strings stands for "format", f-strings are string literals that have an `f` at the beginning and curly braces containing expressions that will be replaced with their values at runtime. To create f-strings, you have to add an `f` or an `F` before the opening quotes of a string.
308248

309249
```python
310-
>>> 'Three zeros and number one: {:04d}'.format(1)
311-
'Three zeros and number one: 0001'
312-
>>> 'Three zeros and number one: %04d' % 1
313-
'Three zeros and number one: 0001'
250+
>>> f"My name is {name} and I'm on the {channel} channel on {network}."
251+
"My name is Akuli and I'm on the ##learnpython channel on freenode."
314252
>>>
315253
```
316254

317-
If you need to know more about formatting I recommend reading
318-
[this](https://pyformat.info/).
319-
320255
## Other things
321256

322257
We can use `in` and `not in` to check if a string contains another

0 commit comments

Comments
 (0)