diff --git a/strings.html b/strings.html index 4fdd03b..6dd4d7a 100644 --- a/strings.html +++ b/strings.html @@ -274,6 +274,28 @@ experience of years.
☞The previous example looks a lot like parsing query parameters in a URL, but real-life URL parsing is actually more complicated than this. If you’re dealing with URL query parameters, you’re better off using the urllib.parse.parse_qs() function, which handles some non-obvious edge cases.
+
Once you’ve defined a string, you can get any part of it as a new string. This is called slicing the string. Slicing strings works exactly the same as slicing lists, which makes sense, because strings are just sequences of characters. +
+>>> a_string = 'My alphabet starts where your alphabet ends.' +>>> a_string[3:11] ① +'alphabet' +>>> a_string[3:-3] ② +'alphabet starts where your alphabet en' +>>> a_string[0:2] ③ +'My' +>>> a_string[:18] ④ +'My alphabet starts' +>>> a_string[18:] ⑤ +' where your alphabet ends.'+
a_string[0]), up to but not including the second slice index (in this case a_string[2]).
+a_string[0:3] returns the first three items of the string, starting at a_string[0], up to but not including a_string[3].
+0, you can leave it out, and 0 is implied. So a_string[:18] is the same as a_string[0:18], because the starting 0 is implied.
+a_string[18:] is the same as a_string[18:44], because this string has 44 characters. There is a pleasing symmetry here. In this 44-character string, a_string[:18] returns the first 18 characters, and a_string[18:] returns everything but the first 18 characters. In fact, a_string[:n] will always return the first n characters, and a_string[n:] will return the rest, regardless of the length of the string.
+⁂
+ operator to concatenate bytes objects. The result is a new bytes object.
bytes object and a 1-byte bytes object gives you a 6-byte bytes object.
bytes object. The items of a string are strings; the items of a bytes object are integers. Specifically, integers between 0–255.
-bytes object is immutable; you can not assign individual bytes. If you need to change individual bytes, you can either use slicing methods (which work the same as strings) and concatenation operators (which also work the same as strings), or you can convert the bytes object into a bytearray object.
+bytes object is immutable; you can not assign individual bytes. If you need to change individual bytes, you can either use string slicing and concatenation operators (which work the same as strings), or you can convert the bytes object into a bytearray object.