编写一个程序,接受一系列空格分隔的单词作为输入,请你反转字符串中 单词的顺序
时间: 2023-05-27 11:03:19 浏览: 119
Solution:
```python
string = input("Enter a string: ")
words = string.split()
words.reverse()
result = " ".join(words)
print("Reversed string: ", result)
```
Explanation:
1. We first take input from the user as a string.
2. We then use the `split()` function to create a list of words from the input string.
3. We use the `reverse()` function to reverse the order of the words in the list.
4. We then use the `join()` function to join the words in the list with a space as the separator.
5. Finally, we print the reversed string using the `print()` function.
相关问题
编写一个程序,接受一系列空格分隔的单词作为输入,请你反转字符串中单词的顺序。
```python
s = input().split()
print(' '.join(s[::-1]))
```
输入样例:
```
hello world
```
输出样例:
```
world hello
```
编写一个python程序,接受一系列空格分隔的单词作为输入,反转字符串中单词的顺序。
```python
s = input("请输入一些单词,单词之间用空格分隔:")
words = s.split()
reversed_words = list(reversed(words))
result = " ".join(reversed_words)
print("反转后的单词顺序为:", result)
```
输入:`hello world my name is John`
输出:`John is name my world hello`
阅读全文