Throughout the course, you've seen me refer to strings ... variables declared like this:
x = 'Hello' # x is a string
It's time to start thinking about what we can do with strings.
You can compare two strings with ==.
We should start by not thinking about it like a variable.
A string is an array of characters. In Python, strings are objects.
An array is a data structure that holds a series of variables with an index. If we think of a variable like a jar with a label and something inside it, it might look like this:
But what about arrays? Think of a series of jars, with one label and an index. Each jar is holding a value.
A string is an array of characters. The string 'HELLO' is actually a series of individual characters 'H','E','L','L','O'.
Once we begin to think of a word, or a sentence, or a paragraph as a series of individual characters, we can begin to have fun with strings.
A substring is a string that may or may not be in a string.
This is the best point in the course to read the Python documentation. Specifically I mean The Python Standard Library, Chapter 4. It contains all the string functions you need to know for string manipulation.
I've gone through the Python string library and picked out some functions that may interest you. Just be aware that I am using most of these functions in their simplest form, and optional parameters are not being used. Please read the documentation before you reinvent the wheel.
In the following examples, I'm dumping the output of the string function to a print() so that you can see it.
# Build strings for later use
mystr = 'abcbdefghija'
mynum = '1234'
mytitle = 'This Is My Title'
dot = '.'
letters = ('A', 'B', 'C')
mess = ' asdasdas dd '
intab = "aeiou"
outtab = "12345"
str = "this is string example....wow!!!"
messy = 'Change The Case'
commaland = 'a,b,c,d,e,f'
print(mystr.capitalize()) # Capitalize first character
print(mystr.center(20, '$')) # Centre text. Pad with provided text
print(mystr.count('a')) # Count occurrences of a substring
print(mystr.endswith('ija')) # Check suffix (ending), return Boolean
print(mystr.find('b')) # Find and return first occurrence of substring, if substring not found return -1
'12' in '1234' # Use instead of find() to verify the substring
print(mynum.isalnum()) # Return true if all characters in the string are alphanumeric and there is at least one character, false otherwise.
print(mystr.isalpha()) # Return true if all characters in the string are alphabetic and there is at least one character, false otherwise.
print(mynum.isdigit()) # Return true if all characters in the string are digits and there is at least one character, false otherwise.
print(mytitle.istitle()) # Return true if the string is a titlecased string and there is at least one character, false otherwise.
print(mytitle.isupper()) # Return true if all cased characters in the string are uppercase and there is at least one cased character,
# false otherwise.
print(dot.join(letters)) # Return a string, which is the concatenation of the strings in the sequence.
print(mytitle.lower()) # Return a copy of the string with all the cased characters converted to lowercase.
print(mess.lstrip()) # Return a copy of the string with leading characters removed.
# The chars argument is a string specifying the set of characters to be removed.
# If omitted or None, the chars argument defaults to removing whitespace.
# The chars argument is not a prefix; rather, all combinations of its values are stripped
# maketrans() links the translation table (links), translate() applies the table
print(str.translate(str.maketrans(intab, outtab)))
print(mystr.replace('a','Q')) # Return a copy of the string with the substrings replaced
print(mystr.rfind('b')) # Find and return last occurrence of substring, if substring not found return -1
print(mess.rstrip()) # Return a copy of the string with trailing characters removed.
# The chars argument is a string specifying the set of characters to be removed.
# If omitted or None, the chars argument defaults to removing whitespace.
# The chars argument is not a prefix; rather, all combinations of its values are stripped
print(commaland.split(',')) # Return a list of the words in the string. With or without delimiter string
print(mystr.startswith('abc')) # Check prefix (beginning), return Boolean
print(mystr.strip('ab')) # Return a copy of the string with the leading and trailing characters removed
print(messy.swapcase()) # Return a copy of the string with uppercase characters converted to lowercase and vice versa.
print(str.title()) # Return a titlecased version of the string where words start with an uppercase character
# and the remaining characters are lowercase.
print(str.upper()) # Return a copy of the string with all the cased characters converted to uppercase.
# FUN STRING STUFF NOT DIRECTLY PART OF THE STRING LIBRARY
print(len(mystr)) # len() returns the length of an object. Strings in Python are objects. Not a String
# library function. Standard Python Library.
print('*' * 10) # use the multiply operator for printing repeated characters
print (messy + mystr) # use the concatenation operator for joining two strings
print(":".join(mystr)) # provide join() a sequence and separate it with the string
# calling join. In this case
# a ':' string called join()
This is a just a glimpse of the Python string library. Knowing it will help you to not reinvent the wheel when you try to solve string based problems.
word = 'sesquipedalian'
i = 0
print(word[i]) # subscript (indexing) of the string
print(word[1]) # positive moves to the right
print(word[-1]) # negative moves to the left, supercool
print(word[i:10]) # slice
print(word[0:2]) # slice characters from position 0 (included) to 2 (excluded)
print(word[2:5]) # slice characters from position 2 (included) to 5 (excluded)
print(word[:2]) # slice character from the beginning to position 2 (excluded)
print(word[4:]) # slice characters from position 4 (included) to the end
print(word[-2:]) # characters from the second-last (included) to the end
One way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the first character numbered 0. Then the right edge of the last character of a string of n characters has index n, for example:
+---+---+---+---+---+---+
| P | y | t | h | o | n |
+---+---+---+---+---+---+
0 1 2 3 4 5 6
-6 -5 -4 -3 -2 -1
The first row of numbers gives the position of the indices 0...6 in the string; the second row gives the corresponding negative indices. The slice from i to j consists of all characters between the edges labeled i and j, respectively.
Inspiration.