Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN PYTHON Implement your own version of the following string methods In this lab

ID: 3587709 • Letter: I

Question

IN PYTHON

Implement your own version of the following string methods


In this lab, we will familiarize ourselves with string functions in Python as well as get practice with function parameters and returning values from a function. As you know, there are many methods that are built into the String data type. Here is an implementation of the count function whose signature is def count(text, aChar).

For this lab, you will re-implement the following string methods in your own python program (string_functions.py) (5 points per function) :

cs110upper - Takes an input string as a parameter and returns it as a string in all uppercase. Hint: Use the ord(c) function to get the ASCII value of a character. For example, ord('a') returns the integer 97. You will also need to use the chr(value) function that returns a string of one character whose ASCII code is the integer value. For example, chr(97) returns the string 'a'. You cannot use the built-in string function upper()for this.

cs110lower - Takes an input string as a parameter and returns it as a string in all lowercase. Hint: Similar to the previous item, use ord() and chr(). Do not use lower() for this.

cs110rfind - Takes an input string and a character as a parameter and returns the first index from the right where the character is found. The original find method finds the first left index of a substring, not just a character.

cs110compare - Takes two input strings as parameters (str1, str2) and

returns a -1 if str1 is less than str2,

returns a zero if str1 is equal to str2, and

returns a +1 if str1 is greater than str2.

cs110strip - Takes an input string as a parameter and returns the same input string with all the whitespace stripped out from the left and the right

cs110replace - Takes an input string and two characters (char1 and char2) as parameters and returns a string with char1 replaced by char2 in the input string.

cs110lstrip - Takes an input string as a parameter and returns the same input string with the whitespace stripped out from the left side of the string

cs110in - Takes an input string and a search string as a parameter and returns either True or False depending on whether the search string is completely in the input string or not.

cs110notin - Takes an input string and a search string as a parameter and returns True if the search string is NOT in the input string. It returns False otherwise.

cs110capitalize - Takes an input string and returns a string with the first character capitalized for every word. The rest of the characters are in lower case. For example, if the input to the function is "I like Python a lot", the function should return a string that looks like "I Like Python A Lot"

Explanation / Answer

cs110upper :

def func(name):
sent = name.split(' ')
final_string = ''
for word in sent:
list_all = list(word)
for letter in list_all:
final_string = final_string + chr(ord(letter) - 32)
final_string = final_string + ' '
return final_string

cs110lower :

def func(name):
sent = name.split(' ')
final_string = ''
for word in sent:
list_all = list(word)
for letter in list_all:
final_string = final_string + chr(ord(letter) + 32)
final_string = final_string + ' '
return final_string

cs110rfind :

def func(name,char):
index = name.rfind(char, 0, len(name))
return index

cs110compare :

def func(name1,name2):

ret = ''
if (name1==name2):
ret = "0"
if (name1<name2):
ret = "-1"
if (name1>name2):
ret = "+1"

return ret

cs110strip :

def func(name):
return name.strip()

cs110replace :

def func(name,char1,char2):
return name.replace(char1,char2)

cs110lstrip :

def func(name):
return name.lstrip()

cs110in :

def func(name,search):
if search in name:
return True
else:
return False

cs110notin :

def func(name,search):
if search not in name:
return True
else:
return False

cs110capitalize :

def func(name):
sen = name.split(' ')
rstring = ''
for word in sen:
rstring = rstring + word.capitalize() + ' '
return rstring