Python Challenge - Reverse Words

def word_reverser(phrase):
return " ".join(reversed(phrase.split()))

print(word_reverser(‘Codecademy rules’))

Hi! This is the way I solved it! :grinning:

def word_reverser(phrase): str_to_list = phrase.split() str_to_list.reverse() final_str = ' '.join(str_to_list) return final_str print(word_reverser('Codecademy rules'))
def word_reverser(phrase): # Write your code here r_phrase = phrase.split(' ') r_phrase.reverse() r_string = '' for word in r_phrase: r_string += word+' ' return r_string print(word_reverser('This is a test of the emergency broadcast system'))
def word_reverser(phrase): # Write your code here theJ= phrase.split() theJ.reverse() return " ".join(theJ) print(word_reverser('What The J'))

all you gotta do is

  1. split the string into a list
  2. reverse the list
  3. join the list into a string

def word_reverser(phrase):

Write your code here

phrase_split = phrase.split(’ ')
new_phrase=list(reversed(phrase_split))
reversed_phrase = ’ '.join(new_phrase)
return reversed_phrase

image

2 Likes

def word_reverser(phrase) :
#split the phrase (type : list)
splitting = phrase.split(sep=" ")

#Rearrange the phrase (type : list)
new_list_of_phrase = splitting[::-1]

#Joining together into one sentence (type : string)
join = " ".join(new_list_of_phrase)
return join

print(word_reverser(‘Codecademy rules’))

def word_reverser(phrase):

Write your code here

phrase_l = phrase.split(" ")
r_phrase =
for index in range(len(phrase_l),0,-1):
print (index)
r_phrase.append(phrase_l[index-1])

print(r_phrase)
return " ".join(r_phrase)

print(word_reverser(‘Codecademy rules’))

def word_reverser(phrase): return " ".join(list(reversed(phrase.split(" ")))) print(word_reverser('Codecademy rules'))

One liner

def word_reverser(phrase):
  # Write your code here
  return " ".join(phrase.split()[::-1])
  

print(word_reverser('Codecademy rules'))
2 Likes

reversed

Hey!

When you do a slice, if you step negatively, we can start counting at the back already? So no need to [-1::-1] ?

Thanks for that.

What was your thought process when you went for a list comprehension? I’m still new to this and I can’t understand how your function works.

def word_reverser(phrase):

Write your code here

phrase = phrase.split()
reversed_phrase = phrase[::-1]
return " ".join(reversed_phrase)

print(word_reverser(‘hello how are you’))

This took me way longer than it should lol LOVING the challenges :smiley:

def word_reverser(n): n = reversed(n.split(" ")) n = " ".join(n) return n print(word_reverser('Codecademy rules'))
def word_reverser(phrase):
  x = reversed(phrase.split(" "))
  return " ".join(x)

print(word_reverser('Codecademy rules'))

def word_reverser(phrase):

Write your code here

reverseList = phrase.split()[::-1]
return (" ".join(reverseList))

print(word_reverser(‘Codecademy rules’))

def word_reverser(phrase):

Write your code here

list_word_rvrs =
list_word_fwd = phrase.split(" ")
while list_word_fwd:
list_word_rvrs.append(list_word_fwd.pop())

return " ".join(list_word_rvrs)

print(word_reverser(‘Codecademy rules’))