gpt-4-0314· OpenAI
WildChat-4.8M (AI2)
Given a string consisting of exactly two words separated by a space. Print a new string…
- User
Given a string consisting of exactly two words separated by a space. Print a new string with the first and second words swapped: the second word is printed first. Consider all adjacent non-space characters a single word. Can you solve it without using if-else and loops? Example input Hello, world! Example output world! Hello, USE STRINGS ONLY
- Assistant
def swap_words(s): words = s.split(" ") return words[1] + " " + words[0]
s = "Hello, world!" print(swap_words(s))