Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (26.4k points)
closed by

Check the below example:

 input:  I live in New York

 output: York New in live I

P.S: I have utilized s[::- 1], this simply turns around the string, as kroY weN ni evil I, yet this isn't the ideal yield. I additionally attempted : 

However, this likewise stands wrong. Compassionately help me recorded as a hard copy of the code. 

closed

4 Answers

0 votes
by (19k points)
 
Best answer
To reverse the word order in a given input string, you can utilize the following code

input_str = "I live in New York"

words = input_str.split()  # Split the input string into individual words

reversed_words = words[::-1]  # Use slicing to reverse the order of the words

output_str = " ".join(reversed_words)  # Join the reversed words back into a string

print(output_str)

In this code snippet, the input string is first split into separate words using the split() method. By applying the [::-1] slicing notation to the words list, the order of the words is reversed. Subsequently, the reversed words are combined back into a string using the join() method, with spaces as the delimiter.

Executing this code will generate the desired output: "York New in live I," representing the reversed word order of the original input string.
0 votes
by (16.4k points)

You can utilize split to get the different words, opposite to reverse the rundown/list, lastly join to go along with them again to make the last string:

s = "This is New York"

# split first

a = s.split()

# reverse list

a.reverse()

# now join them

result = " ".join(a)

# print it

print(result)

Output:

'York New is This'

Wanna become a Python expert? Come and join the python certification course and get certified.

0 votes
by (25.7k points)
It seems that you want to reverse the order of words in a given input string. Here's an example code snippet that achieves the desired output:

input_str = "I live in New York"

words = input_str.split()  # Split the input string into a list of words

reversed_words = words[::-1]  # Reverse the order of words using slicing

output_str = " ".join(reversed_words)  # Join the reversed words back into a string

print(output_str)

This code first splits the input string into a list of words using the split() method. Then, it reverses the order of the words using the slicing notation [::-1]. Finally, it joins the reversed words back into a string using the join() method with a space as the separator.

When you run this code, the output will be: "York New in live I", which is the reversed order of words from the input string.
0 votes
by (15.4k points)
To achieve the desired output of reversing the order of words in a given input string, you can use the following code:

input_str = "I live in New York"

words = input_str.split()  # Split the input string into individual words

reversed_words = words[::-1]  # Reverse the order of the words using slicing

output_str = " ".join(reversed_words)  # Join the reversed words back into a string

print(output_str)

In this code, the input string is first split into a list of words using the split() method. The [::-1] slicing notation is then applied to the words list, effectively reversing the order of the words. The reversed words are then joined back together into a string using the join() method, with spaces as the separator.

When you run this code, it will produce the desired output: "York New in live I", which represents the reversed order of words from the original input string.

Related questions

0 votes
1 answer
asked Jul 18, 2019 in Java by Shubham (3.9k points)
0 votes
1 answer
asked Feb 19, 2021 in Python by laddulakshana (16.4k points)
0 votes
1 answer
asked Jan 2, 2021 in Python by laddulakshana (16.4k points)
0 votes
1 answer
asked Sep 23, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer

Browse Categories

...