如何为字符串中的单词创建单词长度列表?

时间:2019-04-20 20:17:43

标签: python python-3.x

在创建单词长度列表中对我不起作用吗?

     str = "Python is a programming language"
     str = str.split()

     for char in str:
         len(char)
         char = words_in_list
  print(list(words_in_list))

我认为输出必须像这样         [6,2,1,11,8]

1 个答案:

答案 0 :(得分:1)

string = "Python is a programming language"
string = string.split()
my_list = []
for word in string:
    my_list.append(len(word))
print(my_list)