Skip to content

Commit 2e5af27

Browse files
committed
updating stuff
1 parent 26191d1 commit 2e5af27

File tree

1 file changed

+85
-32
lines changed

1 file changed

+85
-32
lines changed

handy-stuff-strings.md

Lines changed: 85 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -146,21 +146,6 @@ Indexing with negative values works like this:
146146

147147
![Indexing with negative values](images/indexing2.png)
148148

149-
## The in keyword
150-
151-
We can use `in` and `not in` to check if a string contains another
152-
string:
153-
154-
```py
155-
>>> "Hello" in our_string
156-
True
157-
>>> "Python" in our_string
158-
False
159-
>>> "Python" not in our_string
160-
True
161-
>>>
162-
```
163-
164149
## String methods
165150

166151
Python's strings have many useful methods. [The official documentation]
@@ -192,6 +177,8 @@ False
192177
' hello 123'
193178
>>> ' hello 123 '.strip() # strip from both sides
194179
'hello 123'
180+
>>> ' hello abc'.rstrip('cb') # strip c's and b's from right
181+
' hello a'
195182
>>> our_string.ljust(30, '-')
196183
'Hello World!------------------'
197184
>>> our_string.rjust(30, '-')
@@ -246,18 +233,6 @@ Python has multiple ways to format strings. One is not necessarily
246233
better than others, they are just different. Here's a few ways to solve
247234
our problem:
248235

249-
- `.format()`-formatting, also known as new-style formatting. This
250-
formatting style has a lot of features, but it's a little bit more
251-
typing than `%s`-formatting.
252-
253-
```py
254-
>>> "Hello {}.".format(name)
255-
'Hello Akuli.'
256-
>>> "My name is {} and I'm on the {} channel on {}.".format(name, channel, network)
257-
"My name is Akuli and I'm on the ##learnpython channel on freenode."
258-
>>>
259-
```
260-
261236
- `%s`-formatting, also known as printf-formatting and old-style
262237
formatting. This has less features than `.format()`-formatting, but
263238
`'Hello %s.' % name` is shorter and faster to type than
@@ -285,16 +260,32 @@ our problem:
285260
Traceback (most recent call last):
286261
File "<stdin>", line 1, in <module>
287262
TypeError: not all arguments converted during string formatting
263+
>>> "we have %s and %s" % ("hello", thestuff)
264+
'we have hello and (1, 2, 3)'
288265
>>> "we have %s" % (thestuff,)
289266
'we have (1, 2, 3)'
290267
>>>
291268
```
292269

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

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+
295284
- f-strings are even less typing, but new in Python 3.6. **Use this only if
296285
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".
286+
than 3.6.** Here the f is short for "format", and the content of the
287+
string is same as it would be with `.format()` but we can use variables
288+
directly.
298289

299290
```py
300291
>>> f"My name is {name} and I'm on the {channel} channel on {network}."
@@ -315,6 +306,67 @@ All of these formatting styles have many other features also:
315306
If you need to know more about formatting I recommend reading
316307
[this](https://pyformat.info/).
317308

309+
## Other things
310+
311+
We can use `in` and `not in` to check if a string contains another
312+
string.
313+
314+
```py
315+
>>> our_string = "Hello World!"
316+
>>> "Hello" in our_string
317+
True
318+
>>> "Python" in our_string
319+
False
320+
>>> "Python" not in our_string
321+
True
322+
>>>
323+
```
324+
325+
We can get the length of a string with the `len` function. The name
326+
`len` is short for "length".
327+
328+
```py
329+
>>> len(our_string) # 12 characters
330+
12
331+
>>> len('') # no characters
332+
0
333+
>>> len('\n') # python thinks of \n as one character
334+
1
335+
>>>
336+
```
337+
338+
We can convert strings, integers and floats with each other with
339+
`str`, `int` and `float`. They aren't actually functions, but they
340+
behave a lot like functions. We'll learn more about what they really
341+
are [later](classes.md).
342+
343+
```py
344+
>>> str(3.14)
345+
'3.14'
346+
>>> float('3.14')
347+
3.14
348+
>>> str(123)
349+
'123'
350+
>>> int('123')
351+
123
352+
>>>
353+
```
354+
355+
Giving an invalid string to `int` or `float` produces an error
356+
message.
357+
358+
```py
359+
>>> int('lol')
360+
Traceback (most recent call last):
361+
File "<stdin>", line 1, in <module>
362+
ValueError: invalid literal for int() with base 10: 'lol'
363+
>>> float('hello')
364+
Traceback (most recent call last):
365+
File "<stdin>", line 1, in <module>
366+
ValueError: could not convert string to float: 'hello'
367+
>>>
368+
```
369+
318370
## Summary
319371

320372
- Slicing returns a copy of a string with indexes from one index to
@@ -327,16 +379,17 @@ If you need to know more about formatting I recommend reading
327379

328380
![Indexing](images/indexing3.png)
329381

330-
- The `in` keyword can be used for checking if a string contains another
331-
string.
332-
333382
- Python has many string methods. Use [the documentation]
334383
(https://docs.python.org/3/library/stdtypes.html#string-methods)
335384
or `help(str)` when you don't rememeber something about them.
336-
337385
- String formatting means adding other things to the middle of a string.
338386
There are multiple ways to do this in Python. You should know how to
339387
use at least one of these ways.
388+
- The `in` keyword can be used for checking if a string contains another
389+
string.
390+
- `len(string)` returns string's length.
391+
- We can use `str`, `int` and `float` to convert values to different
392+
types.
340393

341394
***
342395

0 commit comments

Comments
 (0)