#!/usr/bin/python3 # -*- coding: utf-8 -*- """ Created on Sat Dec 3 18:16:57 2016 @author: taylor@mst.edu """ # Basic iterable reversal: "astr"[::-1] # reversed function "".join(list(reversed("astr"))) # reversed with list comp "".join([char for char in reversed("astr")]) # Manually with a loop: mystr = "astr" accumstring = "" for ichar in range(len(mystr) - 1, -1, -1): accumstring += mystr[ichar] # %% Simple string reversal def reverse_me(s: str) -> str: """ Reverses a string, and returns nothing. """ if s == "": return s else: return reverse_me(s[1:]) + s[0] print("Enter something you'd like backwards:") print(reverse_me("taxes")) # %% Simple string reversal with tab trick def reverse_me_indent(s: str, indent: str = " ") -> str: """ Reverses a string, and returns nothing. """ print(indent, "Starting another call with:", s) if s == "": return s else: return reverse_me_indent(s[1:], indent + " ") + s[0] print("Enter something you'd like backwards:") print(reverse_me_indent("evil"))