@@ -33,9 +33,70 @@ so it creates a new string but it puts it back to the same variable.
33
33
34
34
` + ` and ` * ` are nice, but what else can we do with strings?
35
35
36
+ ## The in keyword
37
+
38
+ We can use ` in ` and ` not in ` to check if a string contains another
39
+ string:
40
+
41
+ ``` py
42
+ >> > " Hello" in our_string
43
+ True
44
+ >> > " Python" in our_string
45
+ False
46
+ >> > " Python" not in our_string
47
+ True
48
+ >> >
49
+ ```
50
+
51
+ ## Indexing
52
+
53
+ Indexing strings is simple. Just type a string or a name of a variable
54
+ name pointing to it, and then whatever index you want inside square
55
+ brackets.
56
+
57
+ ``` py
58
+ >> > our_string[1 ]
59
+ ' e'
60
+ >> >
61
+ ```
62
+
63
+ That's interesting. We got a string that is only one character long. But
64
+ the first character of ` Hello World! ` should be ` H ` , not ` e ` , so why did
65
+ we get an e?
66
+
67
+ Programming starts at zero. Indexing strings also starts at zero. The
68
+ first character is ` our_string[0] ` , the second character is
69
+ ` our_string[1] ` , and so on.
70
+
71
+ So string indexes work like this:
72
+
73
+ ![ Indexing with non-negative values] ( images/indexing1.png )
74
+
75
+ If we index with a negative value Python starts counting from the end of
76
+ the string.
77
+
78
+ ``` py
79
+ >> > our_string[- 1 ]
80
+ ' !'
81
+ >> >
82
+ ```
83
+
84
+ Just like that, we got the last character with -1.
85
+
86
+ But why didn't that start at zero? ` our_string[-1] ` is the last
87
+ character, but ` our_string[1] ` is not the first character!
88
+
89
+ That's because 0 and -0 are equal, so indexing with -0 would do the same
90
+ thing as indexing with 0.
91
+
92
+ Indexing with negative values works like this:
93
+
94
+ ![ Indexing with negative values] ( images/indexing2.png )
95
+
36
96
## Slicing
37
97
38
- Slicing is really simple. It just means getting a part of the string.
98
+ Slicing is like indexing, but instead of getting a string that is one
99
+ character long we usually get a string that is multiple characters long.
39
100
For example, to get all characters between the second place between the
40
101
characters and the fifth place between the characters, we can do this:
41
102
@@ -45,22 +106,24 @@ characters and the fifth place between the characters, we can do this:
45
106
>> >
46
107
```
47
108
48
- So the syntax is like ` some_string[start:end] ` .
109
+ So the syntax is like ` some_string[start:end] ` . The ` : ` is important.
110
+ Square brackets without the ` : ` mean indexing, and square brackets with
111
+ ` : ` mean slicing.
49
112
50
113
This picture shows you how the slicing works:
51
114
52
115
![ Slicing with non-negative values] ( images/slicing1.png )
53
116
54
- But what happens if we slice with negative values?
117
+ So, how does slicing work with negative values?
55
118
56
119
``` py
57
120
>> > our_string[- 5 :- 2 ]
58
121
' orl'
59
122
>> >
60
123
```
61
124
62
- It turns out that slicing with negative values simply starts counting
63
- from the end of the string .
125
+ Seems to be working just like with indexing. As you can see, we don't
126
+ need to worry about what starts from zero and what doesn't .
64
127
65
128
![ Slicing with negative values] ( images/slicing2.png )
66
129
@@ -86,63 +149,46 @@ TypeError: 'str' object does not support item assignment
86
149
>> >
87
150
```
88
151
89
- 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.
91
-
92
- ## Indexing
152
+ ## Slicing with steps
93
153
94
- So now we know how slicing works. But what happens if we forget the ` : ` ?
154
+ There's also a step argument we can give to our slices. It's one by
155
+ default, and it means that the slices contain every character between
156
+ the start and the stop.
95
157
96
158
``` py
97
- >> > our_string[1 ]
98
- ' e'
159
+ ' Hello World!'
160
+ >> > our_string[0 :12 ]
161
+ ' Hello World!'
162
+ >> > our_string[0 :12 :1 ]
163
+ ' Hello World!'
99
164
>> >
100
165
```
101
166
102
- That's interesting. We got a string that is only one character long. But
103
- the first character of ` Hello World! ` should be ` H ` , not ` e ` , so why did
104
- we get an e?
105
-
106
- Programming starts at zero. Indexing strings also starts at zero. The
107
- first character is ` our_string[0] ` , the second character is
108
- ` our_string[1] ` , and so on.
109
-
110
- So string indexes work like this:
111
-
112
- ![ Indexing with non-negative values] ( images/indexing1.png )
113
-
114
- How about negative values?
167
+ Setting the step to something greater than 1 just makes Python skip
168
+ characters. For example, if you set it to 2 Python will get ` H ` , throw
169
+ away ` e ` , get the first ` l ` , throw away the second ` l ` and so on.
115
170
116
171
``` py
117
- >> > our_string[- 1 ]
118
- ' ! '
172
+ >> > our_string[0 : 12 : 2 ]
173
+ ' HloWrd '
119
174
>> >
120
175
```
121
176
122
- We got the last character.
123
-
124
- But why didn't that start at zero? ` our_string[-1] ` is the last
125
- character, but ` our_string[1] ` is not the first character!
126
-
127
- That's because 0 and -0 are equal, so indexing with -0 would do the same
128
- thing as indexing with 0.
129
-
130
- Indexing with negative values works like this:
131
-
132
- ![ Indexing with negative values] ( images/indexing2.png )
177
+ You can also specify the step and leave out everything else.
133
178
134
- ## The in keyword
179
+ ``` py
180
+ >> > our_string[::2 ]
181
+ ' HloWrd'
182
+ >> >
183
+ ```
135
184
136
- We can use ` in ` and ` not in ` to check if a string contains another
137
- string:
185
+ One of the most common ways to use a step is setting it to -1. That
186
+ way Python will go right to left instead of going left to right, and the
187
+ string is reversed.
138
188
139
189
``` py
140
- >> > " Hello" in our_string
141
- True
142
- >> > " Python" in our_string
143
- False
144
- >> > " Python" not in our_string
145
- True
190
+ >> > our_string[::- 1 ]
191
+ ' !dlroW olleH'
146
192
>> >
147
193
```
148
194
@@ -158,7 +204,7 @@ You can run `help(str)` to read it.
158
204
159
205
Remember that nothing can modify strings in-place. Most string methods
160
206
return a new string, but things like ` our_string = our_string.upper() `
161
- still work because the new string is assigned to the old variable.
207
+ still work because the new string is assigned back to the old variable.
162
208
163
209
Here's some of the most commonly used string methods:
164
210
@@ -185,7 +231,7 @@ Here's some of the most commonly used string methods:
185
231
```
186
232
187
233
But that gets a bit complicated if we don' t know the length of the
188
- substring beforehand.
234
+ other string beforehand.
189
235
190
236
```py
191
237
>> > substring = ' Hello'
0 commit comments