Search code examples
pythonpython-3.xlistloopsclass-method

How can I use the string in the list as string methods in python?


I am trying to print some given string in three different ways provided by string methods which are inside a list as strings. Is there a way to convert those strings as methods to use it during loop iteration?

some_string = "hello people!"


string_operations = [".upper()", ".lower()", ".capitalize()"]
for methods in string_operations:
    print(some_string+methods)

Solution

  • In this case you can do something like:

    some_string = "hello people!"
    string_methods = [str.upper, str.lower, str.capitalize]
    for method in string_methods:
        print(method(some_string))