str operations and methods¶When working with multi-line strings, you learned about the newline character: '\n'.
s = """Line one,
line two,
line three."""
s
The backslash character, \, is called the escape character. It can be combined with other characters to form escape sequences. For example, if we want to include a single quote inside a single-quoted string, we use the escape character:
# A failed attempt to include a single quote within a single-quoted string:
'can't'
Now, we use the escape character to to indicate that the quote is a character within the string, not part of the strings opening/closing quotation marks:
'can\'t'
As we see above, we can use double-quotes to include single-quotes in a string, without using the escape character. We can do the same with the types of quotes swapped:
'He said, "I will be there soon".'
"He said, \"I will be there soon\"."
Here is a summary of the escape sequences that we will use most:
\': single quote\": double quote\\: backslash\n: newline\t: tabA string represents a sequence of characters. A string has a length and we can find out the length using built-in method len:
len('happy')
Although they are formed using two symbols, escape sequences count as a single character:
len('\'')
There are also several operators that can be applied to strings, including + (concatenation):
'hi' + 'there'
Another string operator is in, which we use to check whether one string is a substring of another:
'ha' in 'happy'
'hat' in 'happy'
As for numeric values, we can apply comparison operators to strings:
'a' == 'a'
'a' == 'A'
'a' != 'b'
'A' < 'C'
Each character in the string has an index, representing its position within the string. We start counting from 0. For example:
s = 'happy'
s[0]
s[1]
s[2]
s[3]
s[4]
If we try to access an index that doesn't exist, an error occurs:
s[5]
s[12]
An alternative to counting from 0 from left-to-right, is to count from -1 from right-to-left:
s[-1]
s[-2]
s[-3]
s[-4]
s[-5]
As we saw with positive indices, an error occurs when we try to index into a string at a position that doesn't exist:
s[-6]
Using indexing, we can produce a new single-character string. We can use slicing to produce new strings of 0 or more characters, by providing start and stop indices. For example, here we take a slice from start index 2 up to but not including stop index 4:
'computer'[2:4]
Here are a few more examples using both positive and negative indices, including cases where the slice is empty:
'computer'[1:6]
'computer'[3:4]
'computer'[3:3]
'computer'[-4:-2]
'computer'[-2:-4]
We've seen Python's built-in functions. In addition to those functions, each Python types can also have a set of functions defined in them called methods. For example, we can see the str methods by called dir on type str (for now, ignore the ones with underscores in their names):
dir(str)
We can then find out how to use each method, by calling help:
help(str.upper) # Note: we use the type (str) and the method name (upper), separated by a .
Now we can call on the method:
'hello'.upper()
'acdc'.upper()
'c4m'.upper()
In place of the str object, we can use a variable that refers to a str object, such as s:
s = 'hello'
s.upper()
Notice that method upper returned a new uppercase string, but the original str object is unchanged:
s
Strings are immutable, so they cannot be modified.
To call a method, we use this general form:
object.method(arguments)
For all str methods, the object on the left-hand side of the dot will always be a str.
Notice that the string that s refers to hasn't changed, but calling the method upper on s, returned a new string 'HELLO'.
str methods¶Call help on each str method to learn about it and then make an educated guess as to what each of the following method calls produces. Once you've done that, check your work by executing the method calls in the Python shell.
robot = 'R2D2'
robot.isupper()robot.isalpha()robot.isalnum()robot.isdigit()robot.lower()robot.index('2')robot.index('2', 2)robot.count(2)