python编写一个程序,将用户输入的句子”hello world”按照空格分割,然后打印出来
时间: 2023-11-22 12:53:49 浏览: 48
可以使用Python内置的字符串方法split()来实现这个功能,代码如下:
```python
sentence = input("请输入句子:") # 获取用户输入的句子
words = sentence.split() # 将句子按照空格分割成单词
print(words) # 打印单词列表
```
运行程序后,可以输入"hello world",然后程序会将其按照空格分割成两个单词,并打印出来:
```
请输入句子:hello world
['hello', 'world']
```
相关问题
编写程序,输入一个字符串,并将字符串以空格为分割逐行打印出来。
A:
```python
str_input = input("请输入一个字符串:") # 输入字符串
str_list = str_input.split() # 使用split()方法以空格分隔字符串,并保存分隔后的子串到列表str_list中
for sub_str in str_list: # 遍历列表中的子串,并逐行打印
print(sub_str)
```
运行程序后,可输入一个字符串,程序将以空格为分隔符,将字符串逐行打印出来。例如,输入字符串"hello world python",程序将输出:
```
hello
world
python
```
编写一个python程序,从键盘输入一个英文句子,按字母输出每个单词及其重复次数,英文句子中只用空格分割单词,不包括其他标点符号
好的,这是一个编程问题,我可以回答。以下是程序的实现:
```python
sentence = input("请输入一个英文句子:")
words = sentence.split()
count = {}
for word in words:
if word in count:
count[word] += 1
else:
count[word] = 1
for word, count in count.items():
print(word, count)
```
输入一个英文句子后,程序会输出每个单词及其重复次数。例如,输入 "hello world hello python world",输出为:
```
hello 2
world 2
python 1
```