string_play.py

Code is downloadable en bloc on the left, or you can copy and paste this displayed dode. Problem 0 is an example. Have each problem print True if it succeeds using the method shown in Problem 0. No if statements (what is that?) is needed.

Open the Python builtin types page and find the string section. Use it as a reference for solving these problems.


####################################
# Author: PUT YOUR NAME HERE
#  Date created: 18 Aug 2020
#  Date last modified: 18 Aug 2020
#  Program: stringPlay.py
# 
####################################
 
###############Problem 0#################
#   Example problem to show how this works.
#   arg is a string with at least two instances
#   of the letter q
#   Find the substring of r between the first and last q
#########################################
 
arg = "quoque"
start = arg.find("q")
end = arg.rfind("q")
my_result = arg[start + 1: end]
result = "uo"  ##this is my given result
print(result == my_result)  ##prints True if you succeeded.
 
arg = "sissyphus"
# your code goes here.
result = "iyphu"
###############Problem 1#################
#   Remove all instances of the lower-case letter
#   s from a string.  
#########################################
 
arg = "sissyphus"
# your code goes here.
result = "iyphu"
 
###############Problem 2#################
#   arg is guaranteed to have at least two letters
#   and be an alphabetical string
#   Create a string with the first and last letters of arg.
#########################################
 
arg = "heinous"
result = "hs"
 
arg = "Washington"
result = "Wn"
 
###############Problem 3#################
#   head has an even number of letters
#   tail has an even number of letters
#   Create a string whose first half is the first
#   half of head and the second half of tail.
#########################################
 
head = "cerebellum"
tail = "wagger"
result = "cereger"
 
###############Problem 4#################
#   arg is any string
#   Find out how many times the letter b occurs 
#   in arg, case INsensitive
#########################################
 
arg = "Babbage"
result = 3
 
###############Problem 5#################
#   arg is any string
#   Find out how many times the letter b occurs 
#   in the first 10 letters of arg, case INsensitive
#########################################
 
arg = "Babbage"
result = 3
 
arg = "flibbertygibbet"
result = 2
 
###############Problem 6#################
#   arg is any string
#   Find out how many times the letter b occurs 
#   in the first 10 letters of arg, case INsensitive
#########################################
 
arg = "Babbage"
result = 3
 
arg = "flibbertygibbet"
result = 2
 
###############Problem 7#################
#   arg is any string
#   center the string in a string of length 30
#   padded with !s.  
#   if the string has more than 30 characers, just
#   display it.  (no conditional logi on your part needed)
#########################################
 
arg = "wabbit"
result = "!!!!!!!!!!!!wabbit!!!!!!!!!!!!"
 
###############Problem 8#################
#   arg is any string
#   get rid of all of the whitespace on the left, if any
#   replace any spaces on the right with @s.
#########################################
 
arg = "        wabbit    "
result = "wabbit@@@@"