BYU logo Computer Science

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
  1. Write a docstring for the function
  2. Write doctests for the function
  3. Write the function and test it
  4. 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?'
  1. Write a docstring for the function
  2. Write doctests for the function
  3. Write the function and test it
  4. 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 letter
  • at_least_one_upper(s) — true if the string has at least one uppercase letter
  • at_least_one_digit(s) — true if the string has at least one digit
  • at_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:

  1. Write a docstring for the function
  2. Write doctests for the function
  3. Write the function and test it
  4. 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
  1. Write a docstring for the function
  2. Write doctests for the function
  3. Write the function and test it
  4. 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.

  1. Write a docstring for the function
  2. Write doctests for the function
  3. Write the function and test it
  4. 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.

  1. Write a docstring for the function
  2. Write doctests for the function
  3. Write the function and test it
  4. 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.

  1. Write a docstring for the function
  2. Write doctests for the function
  3. Write the function and test it
  4. 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, and if/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.

TaskDescriptionPoints
Digits and spaces onlyYour solution works1
Crazy stringYour solution works1
Password checkerYour solution works1
ShoutYour solution works1
Spam counterYour solution works1
StarsYour solution works1
HashtagsYour solution works1
DocumentationAll functions have good docstrings1
DoctestsAll functions have doctests for useful test cases2