From 350b11f1f7ad6f616bc886cb0bb3daccf7850d13 Mon Sep 17 00:00:00 2001 From: Tushar Khatri Date: Sun, 18 Sep 2022 00:07:46 +0530 Subject: [PATCH 1/3] Replace older formatting methods with f-strings --- basics/handy-stuff-strings.md | 69 ++--------------------------------- 1 file changed, 4 insertions(+), 65 deletions(-) diff --git a/basics/handy-stuff-strings.md b/basics/handy-stuff-strings.md index d3e80a9..ae24e3a 100644 --- a/basics/handy-stuff-strings.md +++ b/basics/handy-stuff-strings.md @@ -241,80 +241,19 @@ 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 +better than others, they are just different. Here's a way to solve our problem: -- `.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 "", line 1, in - 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 +- F-strings is a new formatting mechanism and was introduced 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. - + than 3.6.** Here the `f` is short for "format". f-string is just a string prefixed with `f`. ```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: - -```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' ->>> -``` - -If you need to know more about formatting I recommend reading +If you need to know more about older formatting methods I recommend reading [this](https://pyformat.info/). ## Other things From 8f75683e9b8ae79bf8116e7aadf5430df7d07a74 Mon Sep 17 00:00:00 2001 From: Tushar Khatri Date: Sun, 18 Sep 2022 19:29:02 +0530 Subject: [PATCH 2/3] Explain f-strings and update Python version to 3.6 in README.md --- README.md | 2 +- basics/handy-stuff-strings.md | 12 ++++-------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index f35c025..e3bcca7 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/basics/handy-stuff-strings.md b/basics/handy-stuff-strings.md index ae24e3a..4ef8b0b 100644 --- a/basics/handy-stuff-strings.md +++ b/basics/handy-stuff-strings.md @@ -241,21 +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 way 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. + +`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. -- F-strings is a new formatting mechanism and was introduced 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". f-string is just a string prefixed with `f`. ```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." >>> ``` -If you need to know more about older formatting methods I recommend reading -[this](https://pyformat.info/). - ## Other things We can use `in` and `not in` to check if a string contains another From 173b56d497ab1624c133530666f730afd82d234d Mon Sep 17 00:00:00 2001 From: Tushar Khatri Date: Mon, 19 Sep 2022 01:09:55 +0530 Subject: [PATCH 3/3] Fix Indentation of f-strings example --- basics/handy-stuff-strings.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/basics/handy-stuff-strings.md b/basics/handy-stuff-strings.md index 4ef8b0b..b22bf9c 100644 --- a/basics/handy-stuff-strings.md +++ b/basics/handy-stuff-strings.md @@ -246,11 +246,11 @@ In this tutorial, we will focus on f-strings, which is the most common and usual `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 - >>> 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." - >>> - ``` +```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." +>>> +``` ## Other things