Text/Strings

Lists

Python treats text strings as lists, so you can work with them in much the same way as you would a regular list.

In this exercise, we'll work with Ralph Ellison's (1953) famous opening line:

I am an invisible man.

Notes and Code
Outcome

I. Referencing an item/character in a list

Let's create the string, and print out the 4th character.


                line = "I am an invisible man."
                print(line[3])
              

As we recall, the first item in a list is indexed as 0 so we use line[3] to refer to the 4th item (in this case character) in the list. So the result is:


                m
              
Assignment
Write a program that uses string indexing (like line[3]) to print out:

                      The 11th character is "v"
                    
Answer ▼

II. Concatinating (joining) strings

To join two strings you just use the + operator.


                line = "I am an invisible man."
                author = "Ralph Ellison"
                outLine = line + " by " + author
                print(outLine)
              

We can add quotation marks around the quote, but have to use a different type of quotation mark.


                line = "I am an invisible man."
                author = "Ralph Ellison"
                outLine = '"' + line + '"' + " by " + author
                print(outLine)
              

Assignment
Write a program that uses string indexing (like line[3]) and concatenation (not the f"" formatting) to print out:

                      The 11th character is "v"
                    
Answer ▼

III. Looping through a list/string

We can use a for loop to get each letter in a string. So to count the number of characters in the string we could do:


                line = "I am an invisible man."
                n = 0
                for char in line:
                    n = n + 1
                    print(n, char)
                print(f'There are {n} characters in the string: ' + line)
              

which outputs:


                  1 I
                  2
                  3 a
                  4 m
                  5
                  6 a
                  7 n
                  8
                  9 i
                  10 n
                  11 v
                  12 i
                  13 s
                  14 i
                  15 b
                  16 l
                  17 e
                  18
                  19 m
                  20 a
                  21 n
                  22 .
                  There are 22 characters in the string: I am an invisible man.
                

Assignment
Write a program counts the number of times the letter "a" occurs in the string.
Answer ▼
Code ▼

IV. Splitting Strings

To split the string into separate words we split using the space character (" ") as the seperator.


                line = "I am an invisible man."
                words = line.split(" ")
                print(words)
              

which outputs a list:


                  ['I', 'am', 'an', 'invisible', 'man.']
                

Assignment 1
Write a program that splits the string every time there is a lower case "i".
Answer ▼
Code ▼
Assignment 2
Write a program that splits the string into words and counts the number of words.
Answer ▼
Code ▼

V. Changing Text (Substitution and Removal)

Use the replace method to substitute parts of strings.


                line = "I am an invisible man."
                newLine = line.replace("i", "*")
                print(newLine)
              

which outputs:


                  I am an *nv*s*ble man.
                

You can also replace multiple characters, or replace with no characters to remove text. Here we replace "I am" with "I'm", and remove all the letter "i"'s from the string.


                line = "I am an invisible man."
                print(line)
                line2 = line.replace("I am", "I'm")
                print(line2)
                line3 = line.replace("i", "")
                print(line3)
              

which outputs:


                  I am an invisible man.
                  I'm an invisible man.
                  I am an nvsble man.
                

Assignment 1
Write a program to remove all punctuation marks from the string.
Answer ▼
Code ▼
Assignment 2
Write a program that removes all vowels from the text. (Be sure to look at the code below to see how I do it).
Answer ▼
Code ▼