@@ -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
2929they cannot be changed in-place, and you need to create a new string to
3030change them. Even ` some_string += another_string ` creates a new string.
3131Python will treat that as ` some_string = some_string + another_string ` ,
@@ -87,7 +87,7 @@ TypeError: 'str' object does not support item assignment
8787```
8888
8989There'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
107107first 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+
110124So string indexes work like this:
111125
112126![ Indexing with non-negative values] ( images/indexing1.png )
@@ -151,113 +165,53 @@ True
151165Python's strings have many useful methods. [ The official documentation]
152166(https://docs.python.org/3/library/stdtypes.html#string-methods ) covers
153167them 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
159171Remember that nothing can modify strings in-place. Most string methods
160172return a new string, but things like ` our_string = our_string.upper() `
161173still 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' )
259185True
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