I need a python script to convert every word in a string to uppercase
- User
I need a python script to convert every word in a string to uppercase
Assistant
Sure! Here is a simple Python script that converts every word in a string to uppercase:
def uppercase_words(string): words = string.split() for word in words: word = word.upper() yield word print("".join(uppercase_words("Hello World"))) # Output: HELLO WORLDExplanation:
- The
split()method is used to split the input string into a list of words. - A loop is used to iterate over the words in the list.
- In each iteration, the
upper()method is used to convert the current word to uppercase. - The
yieldkeyword is used to return the uppercase word. - The
print()statement is used to join the uppercase words back into a string and print the result.
Note: This script assumes that the input string is a single sentence or phrase. If the input string contains multiple sentences or phrases, you may need to modify the script to handle them correctly.
- The
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.