I would like to apply the function .lower()
to a string for all of the words that are predefined in a list, but not for any other words.
For instance, take the string provided below.
string1 = "ThE QuIcK BroWn foX jUmpEd oVer thE LaZY dOg."
Now say I have a list as seen below:
lower_list = ['quick', 'jumped', 'dog']
My ideal output would be for the function to apply the .lower()
to the entire string like this:
string1.lower()
And then the output have the function only apply the .lower()
to the instances in string1
that are in the list lower_list
as appears below:
> ThE quick BroWn foX jumped oVer thE LaZY dog.
Can this be done in a simple manner? My idea was to use a for loop, but I need to retain the formatting of the string for example say a string has multiple lines and indents on some lines and not others.
EDIT: I am getting the following error
parts[1::2] = (word.lower() for word in parts[1::2])
AttributeError: 'NoneType' object has no attribute 'lower'
I believe this might be due to have characters other than letters in the strings i use in lower_list
. If I were to have a string like this '(copy)'
then I get the above error. Is there a way to get around this? I was thinking of making every split part into a string using str(xxx)
but not sure how to do that...