Lab 8 — Finding and slicing strings
In this lab, you will write functions that operate on strings. For each function, you will write doctests.
Create a file called finding_and_slicing.py
and you will use this file for all
the problems in this lab.
Digits and spaces only
Write a function called digits_and_spaces_only(s)
. This function takes the
following arguments:
s
: a string
It returns a new string that has only the digits and spaces in the string.
For example:
>>> digits_and_spaces_only("abc83ed5a5p g1 23z")
8355 1 23
- Write a docstring for the function
- Write doctests for the function
- Write the function and test it
- In the main body of the file, call the function 1 time and print the results to the screen.
⚠️ Be sure to test thoroughly, including all kinds of spaces, including tabs and newlines.
Crazy string
Write a function called crazy_string(s)
. This function takes the following
arguments:
s
: a string
It returns a string where every lowercase character in s
is converted to
uppercase, and every other character is converted to lowercase.
For example:
>>> crazy_string("What is GOING on?")
'wHAT IS going ON?'
- Write a docstring for the function
- Write doctests for the function
- Write the function and test it
- In the main body of the file, call the function 1 time and print the results to the screen.
Password checker
Write the following functions:
at_least_one_lower(s)
— true if the string has at least one lowercase letterat_least_one_upper(s)
— true if the string has at least one uppercase letterat_least_one_digit(s)
— true if the string has at least one digitat_least_one_special_character(s)
— true if the string has at least one special character (anything that is not alphanumeric).
Once you have these functions tested, write a function called
password_checker(s)
. This function takes a string and returns true only if all
of the four tests above return true.
For example:
>>> password_checker('Password123!')
True
>>> password_checker('artejfsdwerbfpsA4567aae')
False
Hopefully this illustrates that password checkers aren’t doing much useful. 🙂 The National Institutes of Standards and Technology (NIST) recently modified its password advice to remove this. You can read about this on the auth0 blog or if you really want to read the entire guidance from NIST see this document.
For each function:
- Write a docstring for the function
- Write doctests for the function
- Write the function and test it
- In the main body of the file, call your
password_checker()
function 1 time and print the results to the screen.
Shout
Write a function called shout_score(s)
. This function gives a score to a
string, based on the following rules:
- each exclamation mark ’!’ is 10 points
- each lowercase char is 1 point
- each uppercase char is 2 points
Return the total of all the points for the chars in s
.
For example:
>>> shout_score("Wow that was AMAZING!")
35
- Write a docstring for the function
- Write doctests for the function
- Write the function and test it
- In the main body of the file, call your function 1 time and print the results to the screen.
Spam counter
Write a function called spam_counter(s)
. This function counts how many times
the following words are present in the string: “lottery”, “social security”,
“check”, “offer”, “money”, “sale”, “discount”. These should work regardless of
capitalization in the string. Words don’t double count, so the string
"lottery lottery lottery"
only has a score of 1. The string
"Lottery social Security hello"
has a score of 2. Return the count.
- Write a docstring for the function
- Write doctests for the function
- Write the function and test it
- In the main body of the file, call the function 1 time and print the results to the screen.
Stars
Write a function called find_starred(s)
. This function searches through a
string and returns all characters that are between **
. For examle, the string
"that was **really** good"
should return "really"
. If there is no pattern
like this present, return the empty string. This includes cases where there is a
beginning **
but no ending one.
- Write a docstring for the function
- Write doctests for the function
- Write the function and test it
- In the main body of the file, call the function 1 time and print the results to the screen.
⚠️ Remember, find()
returns -1 if the string is not found.
You will want to use string slicing for this problem.
Hashtags
Write a function called find_hashtag(s)
. This function searches through a
string and returns the hashtag and the word that comes after it. For example, a
string that contains "I'm psyched! #avengers"
should return "#avengers"
.
This hashtag can come anywhere in the string and is terminated by a space or the
end of the string. This string "Wow! #captainmarvel is amazing"
should return
#captainmarvel
. If there is no hashtag present, return the empty string.
- Write a docstring for the function
- Write doctests for the function
- Write the function and test it
- In the main body of the file, call the function 1 time and print the results to the screen.
You will want to use string slicing for this problem.
Lessons
What we want you to get from this lab:
-
You can loop over characters in a string
-
You can use character tests
isalpha()
,isdigit()
,isalnum()
,isspace()
,isupper()
,islower()
, etc. -
You know how to use
if
,if/else
, andif/not
-
You know how to use the early return pattern and the accumulator pattern
-
You know how to test whether a string is present in another string
-
You can use
find()
with a string -
You can slice strings
-
You can put all these concepts together to write functions that process strings
-
You can clearly document your functions with docstrings
-
You can write doctests and use them to test your functions completely
-
You can figure out what went wrong when something unexpected happens
-
Hopefully you had fun!
Points
Turn in a zip file that has your code.
Task | Description | Points |
---|---|---|
Digits and spaces only | Your solution works | 1 |
Crazy string | Your solution works | 1 |
Password checker | Your solution works | 1 |
Shout | Your solution works | 1 |
Spam counter | Your solution works | 1 |
Stars | Your solution works | 1 |
Hashtags | Your solution works | 1 |
Documentation | All functions have good docstrings | 1 |
Doctests | All functions have doctests for useful test cases | 2 |