Strings Worksheet
Some of these problems can be completed very quickly using “advanced” string methods. Try to complete them using only loops, indexing, and basic operators.
Problem 1
Write a function e_counter that takes a string argument and returns the number of how many times the letter e appears in the string.
def e_counter(input_string):
# Your code hereassert e_counter("Hello, world!") == 1
assert e_counter("Welcome to Introduction to Python") == 2
assert e_counter(" ") == 0Problem 2
Write a function letter_counter that uses the same logic as e_counter but modify it to accept two arguments:
- The first argument is the string that will have letters counted
- The second argument is the letter to be counted
Ignore capitalization.
Examples:
letter_counter("ABabCA", "a")should return:3letter_counter("ABabCA", "A")should return:3letter_counter("ABabCA", "c")should return:1
def letter_counter():
# Your code hereassert letter_counter("Hello, world!", "e") == 1
assert letter_counter("burning in water", "r") == 2
assert letter_counter("drowning in flame", "I") == 2
assert letter_counter(" a ", "p") == 0Problem 3
Write a function capitalizer that takes a string argument and returns the same string except every other character is capitalized, starting with the second character. Leave all other capitalization how it is in the original string. Don’t change any non-letter characters, such as spaces or periods.
Examples:
capitalizer("Hello world")returns string'HElLo wOrLd'capitalizer("abcd.e")returns string'aBcD.E'
def capitalizer():assert capitalizer("hello world") == "hElLo wOrLd"
assert capitalizer("HELLO WORLD") == "HELLO WORLD"
assert capitalizer(" ") == (" ")
assert capitalizer("a.bc") == "a.bC"Problem 4
Write a function rounder that takes an argument, a float, and rounds it correctly to the nearest integer. Don’t use any built in rounding methods.
def rounder():
# Your code hereassert rounder(4.5) == 5
assert rounder(3.42) == 3
assert rounder(0.0) == 0
assert rounder(-1.2) == -1Problem 5
Write a function string_spacer that takes a string argument and returns a string. The return string should contain the original string with a space between each character.
*Note: be cautious of a trailing space
Example: string_spacer("hello world") returns: "h e l l o w o r l d"
def string_spacer():
# Your code hereassert string_spacer("Hello world!") == "H e l l o w o r l d !"
assert string_spacer("") == ""
assert string_spacer(" ") == " "
assert string_spacer("ABCD ") == "A B C D "Problem 6
Write a function underliner that takes two string arguments. The second argument will be a substring of the first argument. The function should return a multi-line string of the original string with the substring underlined by the caret ^.
This is hard problem. Harder than any homework or quiz problem. Some hints:
- Create a new, empty string
- Loop through the original string
- Check if the loop is “visiting” the substring
- If the whole substring is eventually, concatenate the correct number of carets to the new string
- If the whole substring isn’t matched, concatenate the correct number of spaces to the new string
- When finish, concatenate the original string and the new string with a newline character (
'\n') between them - Return this output
Example:
underliner("Hello world!", "world") returns:
Hello world!
^^^^^
def underliner():
# Your code hereassert underliner("Hello world!", "world") == "Hello world!\n ^^^^^ "
assert underliner("ABCDBC", "BC") == "ABCDBC\n ^^ ^^"
assert underliner("this is a hard problem", " ") == "this is a hard problem\n ^ ^ ^ ^ "