Skip to content

Commit 323fc3d

Browse files
committed
move stuff around, explain methods
1 parent 3e07341 commit 323fc3d

1 file changed

Lines changed: 18 additions & 12 deletions

File tree

handy-stuff-strings.md

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ Again, nothing can modify strings in-place. Most string methods
158158
return a new string, but things like `our_string = our_string.upper()`
159159
still work because the new string is assigned to the old variable.
160160

161+
Also note that all of these methods are used like `our_string.stuff()`,
162+
not like `stuff(our_string)`. The idea with that is that our string
163+
knows how to do all these things, like `our_string.stuff()`, we don't
164+
need a separate function that does these things like `stuff(our_string)`.
165+
We'll learn more about methods [later](classes.md).
166+
161167
Here's an example with some of the most commonly used string methods:
162168

163169
```py
@@ -233,6 +239,18 @@ Python has multiple ways to format strings. One is not necessarily
233239
better than others, they are just different. Here's a few ways to solve
234240
our problem:
235241

242+
- `.format()`-formatting, also known as new-style formatting. This
243+
formatting style has a lot of features, but it's a little bit more
244+
typing than `%s`-formatting.
245+
246+
```py
247+
>>> "Hello {}.".format(name)
248+
'Hello Akuli.'
249+
>>> "My name is {} and I'm on the {} channel on {}.".format(name, channel, network)
250+
"My name is Akuli and I'm on the ##learnpython channel on freenode."
251+
>>>
252+
```
253+
236254
- `%s`-formatting, also known as printf-formatting and old-style
237255
formatting. This has less features than `.format()`-formatting, but
238256
`'Hello %s.' % name` is shorter and faster to type than
@@ -269,18 +287,6 @@ our problem:
269287

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

272-
- `.format()`-formatting, also known as new-style formatting. This
273-
formatting style has a lot of features, but it's a little bit more
274-
typing than `%s`-formatting.
275-
276-
```py
277-
>>> "Hello {}.".format(name)
278-
'Hello Akuli.'
279-
>>> "My name is {} and I'm on the {} channel on {}.".format(name, channel, network)
280-
"My name is Akuli and I'm on the ##learnpython channel on freenode."
281-
>>>
282-
```
283-
284290
- f-strings are even less typing, but new in Python 3.6. **Use this only if
285291
you know that nobody will need to run your code on Python versions older
286292
than 3.6.** Here the f is short for "format", and the content of the

0 commit comments

Comments
 (0)