How can I split and give uppercase to a string at the same time?
Question:
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’]
Answers:
You don’t even need a separate function, just use split()
and upper()
:
split_sentence = [word.upper() for word in sentence.split()]
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()
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’]
You don’t even need a separate function, just use split()
and upper()
:
split_sentence = [word.upper() for word in sentence.split()]
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()