Skip to content

Commit 92f403d

Browse files
committed
little things
1 parent 7a55fc8 commit 92f403d

2 files changed

Lines changed: 64 additions & 102 deletions

File tree

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ it.**
3131
6. [Using functions](using-functions.md)
3232
7. [If, else and elif](if.md)
3333
8. [ThinkPython: Lists](lists.md)
34-
9. [Loops](loops.md)
34+
9. [Handy stuff with strings](handy-stuff-strings.md)
35+
10. [Loops](loops.md)
36+
37+
Parts of this tutorial that aren't ready to be added to the rest of it
38+
yet:
39+
40+
- [Defining functions](defining-functions.md)
41+
- [Classes](classes.md)
3542

3643
Other things this tutorial comes with:
3744

handy-stuff-strings.md

Lines changed: 56 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ We also know how to repeat them multiple times.
2525
>>>
2626
```
2727

28-
Python strings are **immutable**. That's basically a fancy way to say that
28+
Python strings are **immutable**. That's just a fancy way to say that
2929
they cannot be changed in-place, and you need to create a new string to
3030
change them. Even `some_string += another_string` creates a new string.
3131
Python will treat that as `some_string = some_string + another_string`,
@@ -87,7 +87,7 @@ TypeError: 'str' object does not support item assignment
8787
```
8888

8989
There's also a step argument we can give to our slices, but I'm not
90-
going to talk about it in this tutorial.
90+
going to talk about it now.
9191

9292
## Indexing
9393

@@ -107,6 +107,20 @@ Programming starts at zero. Indexing strings also starts at zero. The
107107
first character is `our_string[0]`, the second character is
108108
`our_string[1]`, and so on.
109109

110+
```py
111+
>>> our_string[0]
112+
'H'
113+
>>> our_string[1]
114+
'e'
115+
>>> our_string[2]
116+
'l'
117+
>>> our_string[3]
118+
'l'
119+
>>> our_string[4]
120+
'o'
121+
>>>
122+
```
123+
110124
So string indexes work like this:
111125

112126
![Indexing with non-negative values](images/indexing1.png)
@@ -151,113 +165,53 @@ True
151165
Python's strings have many useful methods. [The official documentation]
152166
(https://docs.python.org/3/library/stdtypes.html#string-methods) covers
153167
them all, but I'm going to just show some of the most commonly used ones
154-
briefly. **You don't need to remember all of these string methods, just
155-
learn to use the link above so you can find them when you need them.**
156-
Python also comes with built-in documentation about the string methods.
157-
You can run `help(str)` to read it.
168+
briefly. Python also comes with built-in documentation about the string
169+
methods. You can run `help(str)` to read it.
158170

159171
Remember that nothing can modify strings in-place. Most string methods
160172
return a new string, but things like `our_string = our_string.upper()`
161173
still work because the new string is assigned to the old variable.
162174

163-
Here's some of the most commonly used string methods:
164-
165-
- `upper` and `lower` can be used for converting to uppercase and
166-
lowercase.
167-
168-
```py
169-
>>> our_string.upper()
170-
'HELLO WORLD!'
171-
>>> our_string.lower()
172-
'hello world!'
173-
>>>
174-
```
175-
176-
- To check if a string starts or ends with another string we could just
177-
slice the string and compare with to the slice.
178-
179-
```py
180-
>>> our_string[:5] == 'Hello'
181-
True
182-
>>> our_string[-2:] == 'hi'
183-
False
184-
>>>
185-
```
186-
187-
But that gets a bit complicated if we don't know the length of the
188-
substring beforehand.
189-
190-
```py
191-
>>> substring = 'Hello'
192-
>>> our_string[:len(substring)] == substring
193-
True
194-
>>> substring = 'hi'
195-
>>> our_string[-len(substring):] == substring
196-
False
197-
>>>
198-
```
199-
200-
That's why it's recommended to use `startswith` and `endswith`:
201-
202-
```py
203-
>>> our_string.startswith('Hello')
204-
True
205-
>>> our_string.endswith('hi')
206-
False
207-
>>>
208-
```
209-
210-
- If we need to find out where a substring is located, we can do that
211-
with `index`:
212-
213-
```py
214-
>>> our_string.index('World')
215-
6
216-
>>> our_string[6:]
217-
'World!'
218-
>>>
219-
```
220-
221-
- The `join` method joins a list of other strings. We'll talk more about
222-
lists later.
223-
224-
```py
225-
>>> '-'.join(['Hello', 'World', 'test'])
226-
'Hello-World-test'
227-
>>>
228-
```
229-
230-
The `split` method is the opposite of joining, it splits a string to
231-
a list.
232-
233-
```py
234-
>>> 'Hello-World-test'.split('-')
235-
['Hello', 'World', 'test']
236-
>>>
237-
```
238-
239-
- Last but not least, we can use `strip`, `lstrip` and `rstrip` to
240-
remove spaces, newlines and some other whitespace characters from
241-
the end of a string. `lstrip` strips from the left side, `rstrip`
242-
strips from the right side and `strip` strips from both sides.
175+
Here's an example with some of the most commonly used string methods:
243176

244-
```py
245-
>>> ' hello 123 \n '.lstrip()
246-
'hello 123 \n '
247-
>>> ' hello 123 \n '.rstrip()
248-
' hello 123'
249-
>>> ' hello 123 \n '.strip()
250-
'hello 123
251-
>>>
252-
```
253-
254-
It's also possible to combine string methods with slicing and other
255-
string methods:
177+
Here's some of the most commonly used string methods:
256178

257179
```py
258-
>>> our_string.upper()[:7].startswith('HELLO')
180+
>>> our_string.upper()
181+
'HELLO WORLD!'
182+
>>> our_string.lower()
183+
'hello world!'
184+
>>> our_string.startswith('Hello')
259185
True
260-
>>>
186+
>>> our_string.endswith('World!')
187+
True
188+
>>> our_string.endswith('world!') # Python is case-sensitive
189+
False
190+
>>> ' hello 123 \n '.lstrip() # left strip
191+
'hello 123 \n '
192+
>>> ' hello 123 \n '.rstrip() # right strip
193+
' hello 123'
194+
>>> ' hello 123 \n '.strip() # strip from both sides
195+
'hello 123'
196+
>>> our_string.ljust(30, '-')
197+
'Hello World!------------------'
198+
>>> our_string.rjust(30, '-')
199+
'------------------Hello World!'
200+
>>> our_string.center(30, '-')
201+
'---------Hello World!---------'
202+
>>> our_string.count('o') # it contains two o's
203+
2
204+
>>> our_string.index('o') # the first o is our_string[4]
205+
4
206+
>>> our_string.rindex('o') # the last o is our_string[7]
207+
7
208+
>>> '-'.join(['hello', 'world', 'test'])
209+
'hello-world-test'
210+
>>> 'hello-world-test'.split('-')
211+
['hello', 'world', 'test']
212+
>>> our_string.upper()[3:].startswith('LO WOR') # combining multiple things
213+
True
214+
>>>
261215
```
262216

263217
## String formatting
@@ -303,7 +257,8 @@ they are just different. The two ways are:
303257
- `%s`-formatting, also known as printf-formatting and old-style
304258
formatting. This has less features than `.format()`-formatting, but
305259
`'Hello %s.' % name` is shorter and faster to type than
306-
`'Hello {}.'.format(name)`.
260+
`'Hello {}.'.format(name)`. I like to use printf-formatting for simple
261+
things and `.format` when I need more powerful features.
307262

308263
```py
309264
>>> "Hello %s." % name

0 commit comments

Comments
 (0)