python编写一个程序,接受一系列空格分隔的单词作为输入,请你反转字符串中 单词的顺序。
时间: 2023-05-26 14:05:08 浏览: 48
以下是Python程序,可以接受一行以空格分隔的单词,并反转单词顺序:
```python
sentence = input("请输入一行以空格分隔的单词:")
# 将句子转化成单词列表
words = sentence.split()
# 反转单词顺序
words.reverse()
# 将单词列表重新合并成句子
reversed_sentence = " ".join(words)
print("反转后的句子:", reversed_sentence)
```
该程序首先从用户输入中获取一行以空格分隔的单词,并将其转化为单词列表。然后,它使用Python内置的reverse()函数反转列表中单词的顺序。最后,它将反转后的单词列表重新合并成一个句子,并将其打印出来。
相关问题
python编写一个程序,接受一系列空格分隔的单词作为输入,请你反转字符串中单词的顺序。
示例:
输入: "hello world"
输出: "world hello"
输入: "I love python"
输出: "python love I"
代码如下:
string = input("请输入一系列空格分隔的单词:")
words = string.split(" ")
words.reverse()
result = " ".join(words)
print("反转后的单词顺序为:", result)
编写一个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`