Python Lists: .lower() attribute error

后端 未结 2 1481
暖寄归人
暖寄归人 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:17

    Like the error message says, you can't use .lower() on lists, only on strings. That means you'll have to iterate over the list and use .lower() on every list item:

    lowercase = [x.lower() for x in names]
    
    0 讨论(0)
  • 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())
    
    0 讨论(0)
提交回复
热议问题