You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: python/05-Strings.md
+12-10
Original file line number
Diff line number
Diff line change
@@ -50,22 +50,24 @@ text = 'The value of pi is ' + str(pi) ## yes
50
50
51
51
>The "slice" syntax is a handy way to refer to sub-parts of sequences -- typically strings and lists. The slice s[start:end] is the elements beginning at start and extending up to but not including end. Suppose we have s = "Hello"</br>
52
52
53
-
| <center>Function </center> | <center>What it does</center>
54
-
| :------------- | :-------------
55
-
| <a>s[1:4] </a> |is 'ell' -- chars starting at index 1 and extending up to but not including index 4
56
-
| <a>s[1:]</a> |is 'ello' -- omitting either index defaults to the start or end of the string
57
-
| <a>s[:]</a> |is 'Hello' -- omitting both always gives us a copy of the whole thing (this is the pythonic way to copy a sequence like a string or list)
58
-
| <a>s[1:100]</a> |an index that is too big is truncated down to the string length
53
+
| <center>Function </center> | <center>What it does</center>
54
+
| :------------- | :-------------
55
+
| <a>s[1:4] </a> | is 'ell' -- chars starting at index 1 and extending up to but not including index 4
56
+
| <a>s[1:]</a> | is 'ello' -- omitting either index defaults to the start or end of the string
57
+
| <a>s[:]</a> | is 'Hello' -- omitting both always gives us a copy of the whole thing (this is the pythonic way to copy a sequence like a string or list)
58
+
| <a>s[1:100]</a> | an index that is too big is truncated down to the string length
59
+
59
60
</br>
60
61
61
62
>The standard zero-based index numbers give easy access to chars near the start of the string. As an alternative, Python uses negative numbers to give easy access to the chars at the end of the string: s[-1] is the last char 'o', s[-2] is 'l' the next-to-last char, and so on. Negative index numbers count back from the end of the string:</br>
62
63
63
64
| <center>Function </center> | <center>What it does</center>
64
65
| :------------- | :-------------
65
-
| <a>s[-1] </a> |is 'o' -- last char (1st from the end)
66
-
| <a>s[-4]</a> |is 'e' -- 4th from the end
67
-
| <a>s[:-3]</a> |is 'He' -- going up to but not including the last 3 chars
68
-
| <a>s[-3:]</a> |is 'llo' -- starting with the 3rd char from the end and extending to the end of the string
66
+
| <a>s[-1] </a> | is 'o' -- last char (1st from the end)
67
+
| <a>s[-4]</a> | is 'e' -- 4th from the end
68
+
| <a>s[:-3]</a> | is 'He' -- going up to but not including the last 3 chars
69
+
| <a>s[-3:]</a>
70
+
| is 'llo' -- starting with the 3rd char from the end and extending to the end of the string
0 commit comments