len("")
0
Reading: Think Python Chapters 8, 10
You have used strings for some basic operations: creating them, concatenating two strings with +
, and printing them. Strings in Python are substantially more powerful data structures.
''
'a'
is a character'A'
is a different character' '
(space) is also a characterlen
on the string:We can also retrieve individual characters from a string, based on the position of the character.
Counting in Python starts at 0. Just as the range
function starts at 0 by default, the first character of a string is at position 0.
Pay attention to the syntax: the string, followed by an integer in square brackets. The square brackets indicate we are indexing the string; the integer is the position.
Note the variable assignment and indexing on the variable name.
Strings can also be sliced: rather than retrieving a single character, a substring is retrieved. Slicing uses two or three integers: a start position, a stop-before position, and a spacing. This is very similar to range
.
Master the basic slicing syntax first.
We will use a simple list for these examples:
x[1:]
or x[:5]
x[:5]
yields [1, 2, 3, 4, 5]
x[2:]
yields [3, 4, 5, 6, 7, 8]
-1
is the last element, -2
is the second-to-last, etc.x[-2]
yields 7
x[2:-2]
yields [3, 4, 5, 6]
(the second-to-last element, 7, is not included)x[-5:]
yields [4, 5, 6, 7, 8]
x[1:5:2]
yields [2,4]
x[3:0:-1]
yields [4, 3, 2]
x[1:5:-1]
yields []
- an empty list - going backwards from 1 already starts out after the stopping point!x[::-1]
yields [8, 7, 6, 5, 4, 3, 2, 1]
x[::]
yields [1, 2, 3, 4, 5, 6, 7, 8]
- but it’s faster to just use x
🙃Expressions involving strings conform to the same rules we have seen for expressions: they are components that can be used to compose larger expressions and assign values to variables.
len
returns an integer and can be used wherever integers are usedStrings contain built-in functions associated with the string. These associated functions are called methods.
An example is the <str>.upper()
method. Note the syntax: <str>
indicates the method is available for any string:
for
and while
loopsWe can combine these techniques to iterate over characters in a string:
All of the syntax here is syntax you have already learned!
We could also do this with a while
loop:
What is shown above iterates the loop through values and is often called “value iteration.”
It’s also possible to iterate directly over the characters in a string. This can only be done with a for
loop, and is called “content iteration” (because it iterates through the string’s content).
The syntax is very simple:
Strings in Python cannot be changed. They are immutable.
We have seen string concatenation:
The result of concatenation is a new string.
If we were to try to reassign an individual character of a string, we would get an error:
Instead of changing a string, we can compose a new string using characters from the old string:
in
The in
keyword between two strings will return True
or False
depending on whether the first string is in the second string:
This is a very different use of in
than you have seen in for
loops. Trace through this example to see the difference. Note how the conditional is nested inside the loop!
Because computers represent all numbers in memory as binary, it’s common to work with numbers in binary format (base 2), as well as octal (base 8) and hexadecimal (base 16) formats:
Binary (bin), octal (oct), and hexadecimal (hex) are often distinguished from decimal (base 10) with a prefix:
0b
is used for binary0o
is used for octal0x
is used for hexPython can convert from base-10 int to these other formats using bin
, oct
, and hex
functions. The result is a string that includes the prefix:
To convert back, use the int
function with as second parameter: the base:
The len
function also works for lists:
Operators work with lists somewhat similarly to how they work with strings:
+
between two lists concatenates the lists and returns a new list*
Lists have built-in methods, similar to strings. One of the most useful of these is <list>.append()
<list>.sort()
will sort numeric values as you would expect:
[1, 2, 5, 6, 10]
It sorts strings and characters a bit differently:
alpha_sort,py
[' CHICKEN', 'Banana', 'Pear', 'apple', 'mango']
What happened? In Python, every character is associated with an integer. This mapping is known as ASCII. Functions ord()
and chr()
convert between characters and numbers.
Comparing and sorting strings/characters uses these values. You will never need to memorize them.
Iterating through lists is very similar to iterating through strings:
There are some differences, largely because lists are mutable:
Why was no 8
printed?
Very importantly when you iterate directly through the elements of a list with content iteration, the looping variable is a temporary copy of the list element. Changing that temporary variable does not change the list!
On the other hand, using value iteration, you can directly access and change list elements.
When a ‘primitive’ type is assigned to a variable, the value is assigned directly to the variable. When a list is assigned to a variable, the variable becomes a reference to the list. If two variables are assigned to the same list, changing one variable effectively changes the other- since you are changing the list that both variables reference.
Here’s an example:
You can check if two variables reference the same list with the is
keyword:
Similarly, passing a list as an argument to a function passes a reference to this list. If your function modifies the referenced list inside the function, the list is modified outside the function as well.
Note how the add_to_list
function modified the list without returning anything.
Write a function one_star
that takes one argument, a string.
Your function should return a new string, identical to the argument but with the second character changed to the '*'
character.
starred_out('vote')
returns 'v*te'
starred_out('12345')
returns '1*345'
Write a function many_stars
that takes one argument, a string.
Your function should return a new string, identical to the argument but with every character except the first and last changed to the '*'
character.
many_stars('vote')
returns 'v**e'
many_stars('12345')
returns '1***5'
Write a function shorter_string
that takes two arguments, both strings, and returns whichever string is shorter, or the first string if both are the same length.
shorter_string('too much', 'many')
returns 'many'
shorter_string('same', 'okay')
returns 'same'
shorter_string('vowel', 'consonant')
returns 'vowel'
Write a function shared_character
that takes two arguments, both strings, and returns True
if the strings have at least one character in common, or False
if they have no characters in common.
shared_character('true', 'story')
returns True
shared_character('no', 'dice')
returns False
shared_character('blue', 'lagoon')
returns True
shared_character('yes?', 'how?')
returns True
Write a function appendix
that takes as argument a list, appends integer 9
to the list, and returns the list.
appendix(['cat', 'dog'])
should return ['cat', 'dog', 9]
Write a function round_floats
that takes as argument a list of floats. The function should create a new list, consisting of the elements of the argument list, rounded to the nearest integer. Return the new list.
round_floats([1.2, 1.7, 3.1])
should return [1, 2, 3]
round_floats([-1.2, 2.4, 2.5])
should return [-1, 2, 3]
Write a function round_only_floats
that takes as argument a list. The function should create a new list, consisting of the elements of the argument list, with any floats rounded to the nearest integer, and other elements (which are not floats) unchanged. Return the new list.
round_only_floats([1.2, 1.7, 3.1])
should return [1, 2, 3]
round_only_floats([-1.2, 'clouds', 2.5])
should return [-1, 'clouds', 3]
round_only_floats(['mice', 'rats'])
should return ['mice', 'rats']
Write a function positives
that takes as argument a list of ints. The function should create a new list, consisting of the positive elements of the argument list, in the same order. Return the list.
positives([3, -1, 2])
should return [3, 2]
positives([-4, 5, -2, 5])
should return [5, 5]
positives([-1, -8])
should return []
(an empty list)Write a function total_length
that takes as argument a list of strings, and returns the total length of all the strings combined.
total_length(['cat', 'dog'])
should return 6
total_length(['house', 'shed'])
should return 9
total_length(['3', '2'])
should return 2
Homework problems should always be your individual work. Please review the collaboration policy and ask the course staff if you have questions. Remember: Put comments at the start of each problem to tell us how you worked on it.
Double check your file names and printed output. These need to be exact matches for you to get credit.
For this homework, don’t use any built-in functions that find maximum, find minimum, or sort. Also don’t use built-in string methods split
, replace
, or find
.
Write a function shared_stem
that takes two arguments, both strings, and checks if the beginning of the strings are identical. Whatever portion is identical is returned, stopping as soon as any characters are not identical.
shared_stem('look', 'lot')
returns 'lo'
shared_stem('meeting', 'memory')
returns 'me'
shared_stem('late', 'later')
returns 'late'
shared_stem('poor', 'Yorick')
returns ''
(an empty string)Hint: One approach involves iterating over the length of the shorter string, checking both strings to see if the characters are identical, continuing until a non-identical character is found.
Submit as shared_stem.py
.
Write a function remove_vowels
that takes one argument, a string, and returns a new string with all vowels (lowercase or uppercase: a, e, i, o, u, A, E, I, O, U) removed.
remove_vowels('made')
returns 'md'
remove_vowels('valid')
returns 'vld'
remove_vowels('HANDLE')
returns 'HNDL'
remove_vowels('hymn')
returns 'hymn'
Submit as remove_vowels.py
.
Write a function alpha_cat
that takes two arguments, both strings, and returns a single string consisting of the two input strings concatenated in alphabetical order, separated by spaces. The input strings will always consist of only lowercase letters.
alpha_cat('two', 'three')
returns 'three two'
alpha_cat('aa', 'aaaa')
returns 'aa aaaa'
Hint: The comparison operators >
and <
between strings check for alphabetical order for lowercase letters.
Submit as alpha_cat.py
.
Write a function before_comma
that takes one argument, a string. If the string contains any commas, return the portion of the string prior to the comma (or if there are multiple commas, before the first comma). If the string contains no commas, simply return the string.
before_comma("okay, sure")
returns 'okay'
before_comma("Thanks, I think.")
returns 'Thanks'
before_comma("no commas here")
returns 'no commas here'
Hint: You can check for equality between two strings with ==
.
Submit as before_comma.py
.
Write a function list_reverse
that takes as argument a list, and returns a list with the same elements, in reversed order.
list_reverse([2, 3, 5])
returns [5, 3, 2]
list_reverse(["maple", "cherry", "ash", "oak"])
returns ["oak", "ash", "cherry", "maple"]
list_reverse([])
returns []
(an empty list)Submit as list_reverse.py
.
Write a function filter_less
that takes as argument (1) a list of integers and (2) an integer and returns a list containing only the integers from the input list that are less than or equal to the provided integer. The order of elements in the returned list should be the same as their order in the input list.
filter_less([0, 2, 4, 6], 6)
returns [0, 2, 4, 6]
filter_less([1, 2, 3, 4, 5], 1)
returns [1]
filter_less([700], 699)
returns []
Submit as filter_less.py
.
Write a function numbers_first
that takes as argument a list of numbers and/or strings, and returns a list with the same contents, but all numbers come before all strings. Original order should otherwise be preserved.
(“Numbers” here are either ints or floats.)
numbers_first([1, 3.0, 'soap', 0])
returns [1, 3.0, 0, 'soap']
numbers_first(['brooms', 4, 'towels'])
returns [4, 'brooms', 'towels']
numbers_first([15, 2, 'sponge'])
returns [15, 2, 'sponge']
numbers_first([1, 2, 0])
returns [1, 2, 0]
numbers_first(['three', 'two'])
returns ['three', 'two']
Submit as numbers_first.py
.
Write a function zip_finder
that takes as argument a list. Exactly one element of the list will be a zip code: a positive five-digit integer. Return that integer.
Type hint: the return value should be an integer
zip_finder([1, 4, 'Alan', 20052])
returns 20052
zip_finder([False, 20003, -5, "DC"])
returns 20003
zip_finder(["Carl", 854.22, 10001.2, -2000, 86004])
returns 86004
Submit as zip_finder.py
.
Write a function flip_smallest
that takes as argument a list of strings. Find the shortest of these strings and reverse it.
Your function should modify the argument list in place and does not need to return anything. You can assume there will only be one “shortest” string in each list, and that the shortest string will be at least two characters in length.
Examples:
['easy', 'ot', 'say']
['contains', 'plastic', 'strap']
['some', 'kinds', 'era', 'easier', 'than', 'others']
Submit as flip_smallest.py
.
len()
function to check lengths