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 (likeline[3]
) to print out:
The 11th character is "v"
line = "I am an invisible man."
char11 = line[10]
print(f'The 11th character is "{char11}"')
print(f"The 11th character is \"{char11}\"")
)
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 (likeline[3]
) and concatenation (not the f"" formatting) to print out:
The 11th character is "v"
line = "I am an invisible man."
print('The 11th character is ' + '"' + line[10] + '"')
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.
There are 3 "a"s in the string: I am an invisible man.
line = "I am an invisible man."
n = 0
for char in line:
if (char == 'a'):
n = n + 1
print(f'There are {n} "a"s in the string: ' + line)
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".
['I am an ', 'nv', 's', 'ble man.']
line = "I am an invisible man."
words = line.split("i")
print(words)
Assignment 2
Write a program that splits the string into words and counts the number of words.
There are 5 words in the string: I am an invisible man.
You can do this. We believe in you!
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.
I am an invisible man
This one is on you.
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).
m n nvsbl mn.
line = "I am an invisible man."
vowels = "aeiouAEIOU"
for v in vowels:
line = line.replace(v, '')
print(line)