Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Geovany Martinez
7,727 Pointsreverse a string of words in python
Hello,
I hope that someone can help me with this. I am having a logical error. I need the following string to be written backwards. I want something like "Does anything rhyme with orange?" to have an output of "Orange? with rhyme anything does."
This is what I have so far, but the output displays nothing:
def revereString(orgString):
for chr in orgString:
getWord = ""
newStr = ""
getWord = getWord + chr
if chr == " ":
getWord = ""
newStr = getWord + newStr
return "New String: " + newStr
1 Answer
cb123
Courses Plus Student 9,858 PointsI tackled the how to reverse a string, not sure if its optimal. I didn't tackle the nuances of capitalization or punctuation so its not exact. Hopefully the reverse order gets you in the right direction.
''' python def reverseString(orgString):
#Convert String to List and Reverse Order
orgString_to_list = orgString.split()
reverse_list = orgString_to_list[::-1]
reverse_string=""
#Build reverse string from list
for item in reverse_list:
reverse_string += " {}".format(item)
#Output
print(reverse_string)
return "New String:" + reverse_string
str = "Does anything rhyme with orange?" reverseString(str) '''
Geovany Martinez
7,727 PointsGeovany Martinez
7,727 PointsThank you. I was thinking along the lines of converting the string into a list as well. This was an exercise for one of my courses and it states that the program has all the statements needed, but some of them are in the wrong position. I feel like I'm overthinking it and not seeing how simple this fix can be.