编写一个python程序,将一个句子拆分成单词


我在Python编码方面没有太多经验。有人能回答我这个问题吗?
问:编写一个可以将句子拆分成单词的python程序。将每个单词的第一个和最后一个字母更改为大写并显示它们。
例如,如果我们将输入键入为
你好,世界
输出必须为:
你好
世界

# 回答1


到目前为止,你做了什么?
# 回答2

选择 | 换行 | 行号
  1. import re
  2. txt = 'hello,world hello/world hello+world hello-world'
  3. new_word = []
  4. for word in re.split('[ ,+-/]',txt):
  5.     new_word = word[0].upper() + word[1:len(word)-1] + word[len(word)-1].upper()
  6.     print (new_word)

标签: python

添加新评论