Search code examples
pythonstringsplituppercase

How can I split and give uppercase to a string at the same time?


I have this string, I am fairly new to python and I just need help doing this.

def this_sentence(My friend went on a tour)

I want the Output to look like this ['MY', 'FRIEND', 'WENT', 'ON', 'A', 'TOUR']


Solution

  • You can use str.upper() to make all characters uppercase and then str.split() to split the words at each space.

    def this_sentence(sentence):
        return sentence.upper().split()