Skip to content

Replace older formatting methods with f-strings #39

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

Merged
merged 3 commits into from
Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ or very little programming experience. If you have programmed a lot in
the past using some other language you may want to read [the official
tutorial](https://docs.python.org/3/tutorial/) instead.

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

## List of contents
Expand Down
75 changes: 5 additions & 70 deletions basics/handy-stuff-strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,82 +241,17 @@ Instead it's recommended to use string formatting. It means putting
other things in the middle of a string.

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

- `.format()`-formatting, also known as new-style formatting. This
formatting style has a lot of features, but it's a little bit more
typing than `%s`-formatting.

```python
>>> "Hello {}.".format(name)
'Hello Akuli.'
>>> "My name is {} and I'm on the {} channel on {}.".format(name, channel, network)
"My name is Akuli and I'm on the ##learnpython channel on freenode."
>>>
```

- `%s`-formatting, also known as old-style formatting. This has less
features than `.format()`-formatting, but `'Hello %s.' % name` is
shorter and faster to type than `'Hello {}.'.format(name)`. I like
to use `%s` formatting for simple things and `.format` when I need
more powerful features.

```python
>>> "Hello %s." % name
'Hello Akuli.'
>>> "My name is %s and I'm on the %s channel on %s." % (name, channel, network)
"My name is Akuli and I'm on the ##learnpython channel on freenode."
>>>
```

In the second example we had `(name, channel, network)` on the right
side of the `%` sign. It was a tuple, and we'll talk more about them
[later](lists-and-tuples.md#tuples).

If we have a variable that may be a tuple we need to wrap it in another
tuple when formatting:

```python
>>> thestuff = (1, 2, 3)
>>> "we have %s" % thestuff
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting
>>> "we have %s and %s" % ("hello", thestuff)
'we have hello and (1, 2, 3)'
>>> "we have %s" % (thestuff,)
'we have (1, 2, 3)'
>>>
```

Here `(thestuff,)` was a tuple that contained nothing but `thestuff`.

- f-strings are even less typing, but new in Python 3.6. **Use this only if
you know that nobody will need to run your code on Python versions older
than 3.6.** Here the f is short for "format", and the content of the
string is same as it would be with `.format()` but we can use variables
directly.

```python
>>> f"My name is {name} and I'm on the {channel} channel on {network}."
"My name is Akuli and I'm on the ##learnpython channel on freenode."
>>>
```

All of these formatting styles have many other features also:
`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.

```python
>>> 'Three zeros and number one: {:04d}'.format(1)
'Three zeros and number one: 0001'
>>> 'Three zeros and number one: %04d' % 1
'Three zeros and number one: 0001'
>>> f"My name is {name} and I'm on the {channel} channel on {network}."
"My name is Akuli and I'm on the ##learnpython channel on freenode."
>>>
```

If you need to know more about formatting I recommend reading
[this](https://pyformat.info/).

## Other things

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