@@ -33,9 +33,70 @@ so it creates a new string but it puts it back to the same variable.
3333
3434` + ` and ` * ` are nice, but what else can we do with strings?
3535
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+
3696## Slicing
3797
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.
39100For example, to get all characters between the second place between the
40101characters and the fifth place between the characters, we can do this:
41102
@@ -45,22 +106,24 @@ characters and the fifth place between the characters, we can do this:
45106>> >
46107```
47108
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.
49112
50113This picture shows you how the slicing works:
51114
52115![ Slicing with non-negative values] ( images/slicing1.png )
53116
54- But what happens if we slice with negative values?
117+ So, how does slicing work with negative values?
55118
56119``` py
57120>> > our_string[- 5 :- 2 ]
58121' orl'
59122>> >
60123```
61124
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 .
64127
65128![ Slicing with negative values] ( images/slicing2.png )
66129
@@ -86,63 +149,46 @@ TypeError: 'str' object does not support item assignment
86149>> >
87150```
88151
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
93153
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.
95157
96158``` 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!'
99164>> >
100165```
101166
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.
115170
116171``` py
117- >> > our_string[- 1 ]
118- ' ! '
172+ >> > our_string[0 : 12 : 2 ]
173+ ' HloWrd '
119174>> >
120175```
121176
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.
133178
134- ## The in keyword
179+ ``` py
180+ >> > our_string[::2 ]
181+ ' HloWrd'
182+ >> >
183+ ```
135184
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.
138188
139189``` 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'
146192>> >
147193```
148194
@@ -158,7 +204,7 @@ You can run `help(str)` to read it.
158204
159205Remember that nothing can modify strings in-place. Most string methods
160206return 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.
162208
163209Here's some of the most commonly used string methods:
164210
@@ -185,7 +231,7 @@ Here's some of the most commonly used string methods:
185231 ```
186232
187233 But that gets a bit complicated if we don' t know the length of the
188- substring beforehand.
234+ other string beforehand.
189235
190236 ```py
191237 >> > substring = ' Hello'
0 commit comments