CP104 Notes: Strings

Topics


String Indexes

Individual characters in a string may be accessed in exactly the same way that individual elements of a list are accessed: use an integer index to access a character. Unlike lists, strings are immutable, so you cannot change an individual character of a string by referring to its index. Some example:

Code Result Description
s = "Some sentence."
"Some sentence"
String variable creation
s[3]
"e"
Captures fourth character of string
s[999]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range
Generates an error
s[3] = "x"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
Generates an error: strings are immutable

Substrings / Slices

A substring, or slice of a string is accessed by assigning two indexes separated by a ":". (This is identical to list slices.) The values returned start with the first index up to, but not including, the second index. Indexes that are too small, too large, or left out default to 0 and the length of the list. Some examples:

Code Result Description
s = "Some sentence."
"Some sentence"
String variable creation
s[0:1]
"S"
Captures first character of string
s[-100:100]
"Some sentence"
Captures entire string
s[2:9]
"me sent"
Captures substring
s[:]
"Some sentence"
Captures entire string
s[5:]
"sentence"
Captures second word
s[-100:0]
""
Empty string
s[100:-100]
""
Empty string

Traversing Strings

Strings can be traversed - 'walked' through from one end to the other - in three ways, in much the same way that lists can be traversed. The following code shows how the contents of the list:

    
st = "David"

  

can be traversed:

All three versions print the contents of st as:

D
a
v
i
d

Don't Do This:

    
for i in st:
    print(st[i])

  

i is being treated as though it is an index, but it's not. It is the same as the variable c in the previous example - it is actually a character in the string st. What is being done here is the equivalent of attempting to print:

st["D"]
st["a"]
st["v"]
st["i"]
st["d"]

which isn't going to work, because indexes have to be integers, so the loop dies on the first iteration. We strongly recommend that you use i as a variable name for string iteration when it is an index, but not when it is one of the characters in the string. Although you can use any variable name you like, using i for other than an index variable is potentially very confusing.


String Concatenation

Strings can be concatenated: we can to add strings together. Some examples:

Concatenation simply means adding strings together. Strings may be lengthened by adding other strings to them and concatenated by multiplication by using the + and * operators respectively. Some examples:
Code Result Description
          name1 = "David"
name2 = "Brown"
        
String variable creation
full_name = name1 + " " + name2 "David Brown" Concatenating string variables and literals
multi_name = name1 * 4 "DavidDavidDavidDavid" String repetition
multi_name = (name1 + ", ") * 4 "David, David, David, David, " String concatenation and repetition
    
result = ""

while i < n:
    result = result + "*_"
    n += 1

  
"*_*_*_..." Starts with an empty string
Concatenates to the result string until the condition is met, much like a running total

String Methods

String methods are methods that can be applied to strings. The basic format is:

return_value = string.method()

Note that string method calls do not change the current string: rather they return a new string, as in this example:

Code Result Description
name = "David" String variable creation
          
upname = name.upper()
print(name)
print(upname)

        
David
DAVID
Creates an upper case version of name in upname, original string name is unchanged

String Case Methods

There are a number of methods for altering string case:

String Case Methods
Method Purpose
upper() Returns an upper case version of a string.
lower() Returns a lower case version of a string.

Some examples:

Code Result Description
sentence = "This is a sentence."
"This is a sentence."
String variable creation
sentence.upper()
"THIS IS A SENTENCE."
Returns upper-case version of sentence, original variable is unchanged
sentence.lower()
"this is a sentence."
Returns lower-case version of sentence, original variable is unchanged

String Tests

There are a number of methods for testing the contents of a string.
These methods return True or False depending on the state of the string they are testing. These methods are:

String Test Methods
Method Purpose
isalnum() Are all the string's characters letters or digits?
isalpha() Are all the string's characters letters?
isdigit() Are all the string's characters digits?
isspace() Are all the string's characters space characters? (Inc. spaces, tabs, linefeeds, etc.)
isupper() Are all the string's letters in upper-case? (Non-letters are ignored.)
islower() Are all the string's letters in lower-case? (Non-letters are ignored.)

Some examples:

Code Result Description
alphabet = "ABCs"
"ABCs"
String variable creation
alphabet.isalpha()
True
String contains only alphabetic characters
alphabet.isdigit()
False
String contains non-digit characters
alphabet.isupper()
False
String is not all upper-case
"BFG9000".isupper()
True
String alphabetic characters are all upper-case
"BFG9000".isalnum()
True
String alphabetic characters are all alphabetic and digits
" ".isspace()
True
String contains only spaces

String Trimming

There are a number of methods for removing space characters from either side of a string:

String Trim Methods
Method Purpose
strip() Returns string with both leading and trailing spaces removed.
rstrip() Returns string with trailing spaces removed.
lstrip() Returns string with leading spaces removed.

Some examples:

Code Result Description
          string = "       some  words          "
        
Create a string variable
f"|{string}|"
|       some  words          |
String is unchanged
f"|{string}.strip()|"
|some  words|
Removes the spaces around the string
f"|{string.rstrip()}|"
|       some  words|
String has rightmost spaces removed
f"|{string.lstrip()}|"
|some  words          |
String has leftmost spaces removed

Searching Strings

Strings can be searched to see if they contain other strings or substrings. A substring is just another string that may or may not be part of another (usually larger) string. Search methods allow only part of the original string to be searched. This is shown by the syntax

(srch [, start[, end ]])

where srch is the string to search for, start is the starting index within the main string to begin searching, and end is the last index within the main string to search. This is equivalent to segmenting a string with the [start:end] segmentation syntax.

String Search Methods
Method Purpose
count(srch) Returns the number of times srch appears in the string.
find(srch) Returns the index of the first occurrence of srch in the string. Returns -1 if srch is not found.
startswith(prefix) Returns whether the string prefix begins the string (True) or not (False).
endswith(suffix) Returns whether the string suffix ends the string (True) or not (False).

All counts and searches are case-sensitive. Counts and searches operate on non-overlapping substrings. Thus "Wooo!" contains the substring "oo" once, not twice. Note that find returns the index of location of the search string in the main string, not in any substring that may be specified.

Some examples:

Code Result Description
name = "David" Create a string variable
name.count("D")
1
There is 1 "D" in the string
name.count("d")
1
There is 1 "d" in the string
name.upper().count("D")
2
There are 2 "D" in the upper-case version of the string
name[1:4].count("D")
0
There is no "D" in the string
"avi"
name.startswith("D")
True
The string begins with "D" - case is significant
name.find("v")
2
"v"
is at index 2
name.find("x")
-1
"x"
is not in the string

String Comparators

Strings can be compared in the same way that any other objects can be compared in Python, with the <, <=, >, >=, ==, !=, in, not in operators. These operators return True or False. Strings are compared on the basis of the integer ASCII values of their component characters. For example, the letter "A" comes before "a" in the ASCII table. Some examples:

Code Result Description
name = "David" String variable creation
          upname = "DAVID"
name.upper() == upname
        
True
Use case conversion with string comparison
    
char = "i"
is_there = char in "David"

  
True
Comparisons can be done with, and saved to, variables
"Brown" < "Browne"
True
Shorter strings come before longer strings that match up to the length of the shorter string
"a" < "A"
False
Upper-case letters come before lower-case letters in comparisons

The in Operator

The in operator determines if a specific substring is found in a string or not, and returns True or False respectively. It is used as:

substring in string

Examples:

Code Result Description
name = "David"
"David"
String variable creation
"a" in name
True
name contains
"a"
"z" in name
False
name does not contain
"z"
    
c = "vi"
found = c in st

  
True
Result stored in boolean variable
    
b = 'Hello'
found = b in st

  
False
Result stored in boolean variable