Turn in on the submit serverbefore the end of lecture for credit
Turn in one .zip with all the .py files
Unlimited attempts during lecture
We’ll provide solutions – pay attention!
Common Errors
Incorrect file names:
check_even.py vs. checkEven.py
Incorrect function name
def check_even(x): vs. def check_evens(x):
Spacing errors
1 2 3 vs. 1 2 3
Read your autograder output!
Today
Goal: Level up our string knowledge
So far we’ve created strings, concatenated strings, printed and returned them
Today:
Getting the number of characters in a string
Accessing characters and substrings of a string
Iterating across strings using for and while loops
Strings are immutable; creating new strings
String methods (lower case, upper case)
A string is a sequence or collection of characters
t ="hello"
x ="concatenate"
v ="hello"+" "+"world"
s =""
Getting the length of a string
You can use len() to get the number of characters in a string:
s ="hello"len(s)
5
s =""len(s)
0
Accessing characters in a string
You can retrieve a single character from a string:
s ="hello"s[0]
'h'
"hello"[1]
'e'
s ="hello"s[len(s)-1]
'o'
Common Indexing Error
If you get a IndexError: string index out of range error, you are likely trying to access an index that doesn’t exist in the string, e.g.,
"hello"[5]
"hello"[6]
""[1]
String slices
You can also retrieve a substring
Uses: a start position, a stop-before position, and a spacing; separated by colons
s ="concatenate"s[1:3]
'on'
s ="concatenate"s[0:3:2]
'cn'
More examples of string slices
You can also retrieve a substring
Uses two or three integers: a start position, a stop-before position, and a spacing; separated by colons
s ="concatenate"s[1:]
'oncatenate'
s ="concatenate"s[:4]
'conc'
In-Class Exercise
Write a function ex1(s) that takes one string as its argument.
If the string has 0 characters, return the empty string.
Otherwise, if the string has strictly fewer than four characters (3 or less), return the first character of the string.
Otherwise, return the 4th character in the string.
ex1.py
def ex1(s):# Your code goes here
ex1("abcdefg") should return "d"
ex1("abc") should return "a"
ex1("") should return ""
Strings are immutable
Strings in Python cannot be changed. They are immutable.
s ='hello's[0] ='j'
Will give: TypeError: 'str' object does not support item assignment
Instead, we can make new strings:
s ='hello'new_s ='j'+ s[1:]print(new_s)
jello
Iterating over strings with for loops
For-loops let us iterate over collections of items
We can iterate over characters in a string
s ='hello'for i inrange(0, len(s)):print(s[i])
h
Iterating over strings with for loops
For-loops let us iterate over collections of items
We can iterate over characters in a string
s ='hello'for i inrange(0, len(s)):print(s[i])
h
e
Iterating over strings with for loops
For-loops let us iterate over collections of items
We can iterate over characters in a string
s ='hello'for i inrange(0, len(s)):print(s[i])
h
e
l
Iterating over strings with for loops
For-loops let us iterate over collections of items
We can iterate over characters in a string
s ='hello'for i inrange(0, len(s)):print(s[i])
h
e
l
l
Iterating over strings with for loops
For-loops let us iterate over collections of items
We can iterate over characters in a string
s ='hello'for i inrange(0, len(s)):print(s[i])
h
e
l
l
o
Iterating over strings with for loops
For-loops let us iterate over collections of items
We can iterate over characters in a string
s ='hello'for i inrange(0, len(s)):print(s[i])
h
e
l
l
o
Iterating over strings with while loops
We can do the same thing with a while loop
s ='hello'j =0while j <len(s):print(s[j]) j = j +1
h
Iterating over strings with while loops
We can do the same thing with a while loop
s ='hello'j =0while j <len(s):print(s[j]) j = j +1
h
e
Iterating over strings with while loops
We can do the same thing with a while loop
s ='hello'j =0while j <len(s):print(s[j]) j = j +1
h
e
l
Iterating over strings with while loops
We can do the same thing with a while loop
s ='hello'j =0while j <len(s):print(s[j]) j = j +1
h
e
l
l
Iterating over strings with while loops
We can do the same thing with a while loop
s ='hello'j =0while j <len(s):print(s[j]) j = j +1
h
e
l
l
o
Iterating over strings with while loops
We can do the same thing with a while loop
s ='hello'j =0while j <len(s):print(s[j]) j = j +1
h
e
l
l
o
We can also iterate directly over the characters in a string
But this can only be done using a for-loop
New syntax:
s ='hello'for char in s:print(char)
h
We can also iterate directly over the characters in a string
But this can only be done using a for-loop
New syntax:
s ='hello'for char in s:print(char)
h
e
We can also iterate directly over the characters in a string
But this can only be done using a for-loop
New syntax:
s ='hello'for char in s:print(char)
h
e
l
We can also iterate directly over the characters in a string
But this can only be done using a for-loop
New syntax:
s ='hello'for char in s:print(char)
h
e
l
l
We can also iterate directly over the characters in a string
But this can only be done using a for-loop
New syntax:
s ='hello'for char in s:print(char)
h
e
l
l
o
We can also iterate directly over the characters in a string
But this can only be done using a for-loop
New syntax:
s ='hello'for char in s:print(char)
h
e
l
l
o
Looping and Counting
What does the following code print?
s ='hello'count =0for j in s: count = count +1print(count)
5
What does this code do at a high level?
Same thing as len(): it prints the length (number of characters) of string s
In-Class Exercise
Write a function ex2(s) that takes as its argument one string (that will be at least one character long) and returns a new string that has a space after every character in the original string.
ex2.py
def ex2(s):# Write a loop that makes a new string with a space after ever character# Return this new string
ex2("abcdefg") should return "a b c d e f g "
ex2("abc") should return "a b c "
ex2(" ") (single space) should return " " (two spaces)
in
The keyword in will return True or False depending on whether the first string is in the second string
'cat'in'concatenate'
True
'ce'in'concatenate'
False
'a'in'aeiou'
True
Other fun string things
You can make strings upper case
'csci1012'.upper()
'CSCI1012'
You can make strings lower case
'YES'.lower()
'yes'
Other fun string things
You can check for string equality
s ='happy''happy'== s
True
You can compare alphabetical order of strings (in lowercase) using < and >
'happy'<'sad'
True
In-Class Exercise
Write a function ex3(s) that takes as input one string, s. If the string contains the character ‘!’, return the string in all capital letters; otherwise, return it as is.
ex3.py
def ex3(s):# Your code goes here
ex3("!abcdefg") should return "!ABCDEFG"
ex3("abc") should return "abc"
ex3("") should return ""
ex3("yay!") should return "YAY!"
Summary
Goal: Level up our string knowledge
Getting the number of characters in a string
Accessing characters and substrings of a string
Iterating across strings using for and while loops