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
+19-18
Original file line number
Diff line number
Diff line change
@@ -33,16 +33,17 @@ text = 'The value of pi is ' + str(pi) ## yes
33
33
34
34
<h2>Functions</h2>
35
35
36
-
| <center>Function </center> | <center>What it does</center>
37
-
| :------------- | :-------------
38
-
| <a>s.lower(), s.upper()</a> |Returns the lowercase or uppercase version of the string
39
-
| <a>s.strip()</a> |Returns a string with whitespace removed from the start and end
36
+
| <center>Function </center> | <center>What it does</center>
37
+
| :------------- | :-------------
38
+
| <a>s.lower(), s.upper()</a> | Returns the lowercase or uppercase version of the string
39
+
| <a>s.strip()</a> |Returns a string with whitespace removed from the start and end
40
40
| <a>s.isalpha(), s.isdigit(), s.isspace()</a> |Tests if all the string chars are in the various character classes
41
-
| <a>s.startswith('other'), s.endswith('other')</a> |Tests if the string starts or ends with the given other string
42
-
| <a>s.find('other')</a> |Searches for the given other string (not a regular expression) within s, and returns the first index where it begins or -1 if not found
43
-
| <a>s.replace('old', 'new') </a> |Returns a string where all occurrences of 'old' have been replaced by 'new'
44
-
| <a>s.split('delim') </a> |Returns a list of substrings separated by the given delimiter. The delimiter is not a regular expression, it's just text. 'aaa,bbb,ccc'.split(',') -> ['aaa', 'bbb', 'ccc']. As a convenient special case s.split() (with no arguments) splits on all whitespace chars.
45
-
| <a>s.join(list) </a> |Opposite of split(), joins the elements in the given list together using the string as the delimiter. e.g. '---'.join(['aaa', 'bbb', 'ccc']) -> aaa---bbb---ccc
41
+
| <a>s.startswith('other'), s.endswith('other')</a> |Tests if the string starts or ends with the given other string
42
+
| <a>s.find('other')</a> |Searches for the given other string (not a regular expression) within s, and returns the first index where it begins or -1 if not found
43
+
| <a>s.replace('old', 'new') </a> |Returns a string where all occurrences of 'old' have been replaced by 'new'
44
+
| <a>s.split('delim') </a> |Returns a list of substrings separated by the given delimiter. The delimiter is not a regular expression, it's just text. 'aaa,bbb,ccc'.split(',') -> ['aaa', 'bbb', 'ccc']. As a convenient special case s.split() (with no arguments) splits on all whitespace chars.
45
+
| <a>s.join(list) </a> |Opposite of split(), joins the elements in the given list together using the string as the delimiter. e.g. '---'.join(['aaa', 'bbb', 'ccc']) -> aaa---bbb---ccc
46
+
46
47
</br>
47
48
48
49
<h2>String Slices</h2>
@@ -51,20 +52,20 @@ text = 'The value of pi is ' + str(pi) ## yes
51
52
52
53
| <center>Function </center> | <center>What it does</center>
53
54
| :------------- | :-------------
54
-
| <a>s[1:4] </a> |is 'ell' -- chars starting at index 1 and extending up to but not including index 4
55
-
| <a>s[1:]</a> |is 'ello' -- omitting either index defaults to the start or end of the string
56
-
| <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)
57
-
| <a>s[1:100]</a> |an index that is too big is truncated down to the string length
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
58
59
</br>
59
60
60
61
>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>
61
62
62
63
| <center>Function </center> | <center>What it does</center>
63
64
| :------------- | :-------------
64
-
| <a>s[-1] </a> |is 'o' -- last char (1st from the end)
65
-
| <a>s[-4]</a> |is 'e' -- 4th from the end
66
-
| <a>s[:-3]</a> |is 'He' -- going up to but not including the last 3 chars
67
-
| <a>s[-3:]</a> |is 'llo' -- starting with the 3rd char from the end and extending to the end of the string
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
68
69
</br>
69
70
70
71
<h2>String %</h2>
@@ -84,4 +85,4 @@ text = "%d little pigs come out or I'll %s and %s and %s" % (3, 'huff', 'puff',
84
85
## add parens to make the long-line work:
85
86
text = ("%d little pigs come out or I'll %s and %s and %s"%
0 commit comments