I need to write a function that takes a string as an argument and outputs the letters backward, one per line.so far I have this:

def wordReverse(word):
	acum = ""
	for ch in word:
		acum = ch + acum
	return acum

I tried adding \n to acum but an error occurs.Where should I put it?

nevermind. I got it. here's what I did:

def wordReverse(word):
	acum = ""
	for ch in word:
		acum= ((ch)+"\n") + acum
	print acum
def wordReverseToLines(word):
  for i in range(len(word)-1,-1,-1):
    print (word[i])
# or
def wordReverseUsingList(word):
  arr = [x for x in word]
  arr.reverse()
  print ("\n".join(arr))

Edited 6 Years Ago by griswolf: n/a

nevermind. I got it. here's what I did:

def wordReverse(word):
	acum = ""
	for ch in word:
		acum= ((ch)+"\n") + acum
	print acum

Great, very nice solution!

You can use Python built-in functions to simplify it a little ...

def wordReverse(word):
    for ch in reversed(word):
        print ch

wordReverse("python")

Edited 6 Years Ago by vegaseat: show off

And for the beauty of recursion you can try:

def wordReverse(word):
    if not word:
        return word ## empty sequence is already reversed
    else:
        return word[-1]+wordReverse(word[:-1])

print wordReverse('Python newbie')

And the classic "vegaseat's"

def wordReverse(word):
    return word[::-1] # -1 step from default start to default end

Edited 6 Years Ago by pyTony: n/a

This question has already been answered. Start a new discussion instead.