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: basics/handy-stuff-strings.md
+5-70Lines changed: 5 additions & 70 deletions
Original file line number
Diff line number
Diff line change
@@ -241,82 +241,17 @@ Instead it's recommended to use string formatting. It means putting
241
241
other things in the middle of a string.
242
242
243
243
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.
246
246
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: notall 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.
308
248
309
249
```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."
314
252
>>>
315
253
```
316
254
317
-
If you need to know more about formatting I recommend reading
318
-
[this](https://pyformat.info/).
319
-
320
255
## Other things
321
256
322
257
We can use `in` and `not in` to check if a string contains another
0 commit comments