Ask coding questions

← Back to all posts
Deleting a word in a string in Python
chukaobi231 (0)

Good day,

I am struggling with the exercise below, please I need help.

Complete the function to remove the word WGU from the given string

ONLY if it's not the first word and return the new string

def removeWGU(mystring):

Student code goes here

expected output: WGU Rocks

print(removeWGU('WGU Rocks'))

expected output: Hello, John

print(removeWGU('Hello, WGUJohn'))

My code:
def removeWGU(mystring):
mystring.find('WGU')
if 'WGU' != 0:
return mystring.replace('WGU','')

Can somebody help me out? I am stuck with this. Thank you.

Comments
hotnewtop
dan3093 (0)

Try this:

Complete the function to remove the word WGU from the given string

ONLY if it's not the first word and return the new string

def removeWGU(mystring):

Student code goes here

string_list = []
string_list = mystring.split(" ")
if string_list[0] != "WGU":
    clean = []
    for word in string_list:
        clean.append(word.replace("WGU", ""))
    return " ".join(clean)    
else:
    return " ".join(string_list)
        

expected output: WGU Rocks

print(removeWGU('WGU Rocks'))

expected output: Hello, John

print(removeWGU('Hello, WGUJohn'))

Bookie0 (6297)

um it seems to me that you want to take of WGU off WGU rocks; it appears you did that right?:

hope this helps! =)