Python Lists: .lower() attribute error

后端 未结 2 1512
暖寄归人
暖寄归人 2021-01-28 09:12

I am trying to create a program on python to do with manipulating lists/arrays. I am having trouble with an error:

lowercase = names.lower

2条回答
  •  天命终不由人
    2021-01-28 09:31

    The variable names is a list. You can't use the .lower() method on a list.

    pp_ provided the solution:

    lowercase = [x.lower() for x in names]
    

    While not exactly equivalent to the previous example, this may read better to you and has, effectively, the same result:

    lowercase=[]
    for name in names:
        lowercase.append(name.lower())
    

    Alternate solution that may fit your needs:

    print (str(names).lower())
    

提交回复
热议问题