An excellent list of string methods is here:http://rgruet.free.fr/PQR27/PQR2.7.html#stringMethods In fact, bookmark the page (I use it frequently): http://rgruet.free.fr/PQR27/PQR2.7.html |
|
The in operator is used to tell whether a substring (or single character) is inside a target string | 'h' in 'hi there' →
True 'h' in 'Hi there' → False 'the' in 'hi there' → True 'it' in 'hi there' → False |
ord(c) will return the
ASCII code for the single
character c The normal characters start at value 32 (for a space) with the ranges: 65 - 90 for A - Z 97 - 122 for a - z 48 - 57 for the characters 0 - 9 and lots of puncutation interspersed, and not many obsolete control-codes as well |
ord('A')
→ 65 ord('2') → 50 ord('b') → 98 ord('+') → 43 |
chr(n) will return the
character than has the ASCII code n this is the inverse function to ord() |
chr(65)
→ 'A' chr(43) → '+' chr(ord('A')) → 'A' ord(chr(65)) → 65 |
The string method .find(t)
will return the position of the first occurrence of the string t inside
the string calling the method. Or return -1 if not found. |
q = 'Hi there' q.find('i') → 1 q.find('the') → 3 q.find('hi') → -1 q.find('red') → -1 q.find('i t') → 1 'George'.find('or') → 2 |
The string method .count(t)
will return the number of occurrences of the string t
inside the string calling the method. |
q = 'therefore this' q.count('e') → 3 q.count('th') → 2 q.count('fort') → 0 |