Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Create an empty list to store the letters of the string
- letters = []
- #Define a function that collects the letters of a string and appends them into a list
- def collect_string():
- word = input("word to reverse:")
- for i in word:
- letters.append(i)
- return letters
- #Create an empty list to store each letter from last to first
- def rev_string(string):
- rev = []
- size = len(letters)
- for i in letters:
- if size > 0:
- rev.append(letters[size-1])
- size -= 1
- #at the end of the function, use the join method to concatenate the reversed list back into a string; "" specifies no gaps between list items
- back = "".join(rev)
- return back
- #Call the function anf print the output
- print(rev_string(collect_string()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement