Assigning and Calling built-in functions from a dictionary Python
Method #1:
Use operator.methodcaller
from operator import methodcaller
commands = {"1": methodcaller('upper'),
"2": methodcaller('lower'),
"3": methodcaller('replace', " ", "\n")}
user_string = input("Enter a string: ")
option = input("How would you like to alter your string? (Choose one of the following:)\
\t1. Make all characters capital\
\t2. Make all characters lowercase")
result = commands[option](user_string)
Method #2:
commands = {"1" : str.upper, "2" : str.lower, "3" : lambda string: str.replace(string, " ", "\n")} # use a lambda here to pass extra parameters
user_string = input("Enter a string: ")
option = input("How would you like to alter your string? (Choose one of the following:)\
\t1. Make all characters capital\
\t2. Make all characters lowercase")
new_string = commands[option](user_string)
Method #3:
command = lambda a:{"1" : a.upper(), "2" : a.lower(), "3" : a.replace(" ",'\n')}
user_string = input("Enter a string: ")
options = input("How would you like to alter your string? (Choose one of the following:)")
print("output:",command(user_string)[options])
#\t1. Make all characters capital\n