用python 假设有字符串如下: s="Man is the master of his destiny." 编写程序,提取出字符串中的单词,并按照每行一个单词的格式输出。
时间: 2024-04-30 10:19:21 浏览: 11
可以使用字符串方法`split()`来将字符串分割成单词,然后使用`for`循环逐个输出。
代码如下:
```python
s = "Man is the master of his destiny."
words = s.split() # 将字符串分割成单词
for word in words:
print(word)
```
输出结果为:
```
Man
is
the
master
of
his
destiny.
```
如果需要每行一个单词,可以按照如下方式修改代码:
```python
s = "Man is the master of his destiny."
words = s.split() # 将字符串分割成单词
for word in words:
print(word, end='\n')
```
输出结果为:
```
Man
is
the
master
of
his
destiny.
```
其中`end='\n'`表示在每个单词后面输出一个换行符,从而实现每行一个单词的效果。