Knowee
Questions
Features
Study Tools

Reverse words in a given String in Pythonstring = "Hello World"print(string[::-1])string = "Hello World"print(" ".join(string.split()[::-1]))string = "Hello World"print(" ".join(reversed(string)))string = "Hello World"print(" ".join(string.split()[::1]))

Question

Reverse words in a given String in Pythonstring = "Hello World"print(string[::-1])string = "Hello World"print(" ".join(string.split()[::-1]))string = "Hello World"print(" ".join(reversed(string)))string = "Hello World"print(" ".join(string.split()[::1]))

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here's the explanation for each line of code:

  1. string = "Hello World": This line is initializing a string variable with the value "Hello World".

  2. print(string[::-1]): This line is printing the string in reverse order. The [::-1] is a slice that starts at the end of the string, and moves backwards.

  3. string = "Hello World": This line is re-initializing the string variable with the value "Hello World".

  4. print(" ".join(string.split()[::-1])): This line is splitting the string into a list of words, reversing that list, and then joining them back together with spaces in between. The result is the words in the string printed in reverse order.

  5. string = "Hello World": This line is re-initializing the string variable with the value "Hello World".

  6. print(" ".join(reversed(string))): This line is using the built-in reversed() function to reverse the string, and then joining the characters back together with spaces in between. The result is the string printed in reverse order, with each character separated by a space.

  7. string = "Hello World": This line is re-initializing the string variable with the value "Hello World".

  8. print(" ".join(string.split()[::1])): This line is splitting the string into a list of words, keeping the order of that list, and then joining them back together with spaces in between. The result is the original string printed as is.

This problem has been solved

Similar Questions

python program to reverse a string

What is the output of the following code?  word = "Python" print(word[::-1])

Write a Python program to split a string to wordsstring = "Hello World"print(string.split())string = "Hello World"print(string.split("o"))string = "Hello World"print(string.split("l"))string = "Hello World"print(string.split("W"))

Word Reversal in a StringDesign a Python program to accept a string from user.The string can be composed of words separated by a spaces. Implement the code logic to modify and return the given string ‘str’ such that its every word is inverted but appears in the same order as the original string.Constraints:NAExample:Sample Input:Python StringsSample Output:nohtyP sgnirtS

Given an input string s, reverse the order of the words.A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.Return a string of the words in reverse order concatenated by a single space.Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces. Example 1:Input: s = "the sky is blue"Output: "blue is sky the"Example 2:Input: s = " hello world "Output: "world hello"Explanation: Your reversed string should not contain leading or trailing spaces.Example 3:Input: s = "a good example"Output: "example good a"Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string. Constraints:1 <= s.length <= 104s contains English letters (upper-case and lower-case), digits, and spaces ' '.There is at least one word in s. Follow-up: If the string data type is mutable in your language, can you solve it in-place with O(1) extra space?

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.