我们希望的正常的使用:
下面示例是字符串所以可以使用!
>>> k = 'good blue sky'
>>> k.capitalize()
'Good blue sky'
1
2
3
报错的是因为你创建的不是字符串,可能是一个列表!
类似这样:
>>> ss = ['mode','in','china']
>>> ss.capitlize()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'capitlize'
1
2
3
4
5
那么就要进行修改啦:
>>> [string.capitalize() for string in ss]
['Mode', 'In', 'China']
>>>
把列表变成sting就行啦!
类似集合,元组,字典都可以!
以下为示例:
>>> d = {'sd','blue'}#集合
>>> d.capitlize()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'set' object has no attribute 'capitlize'
>>> [string.capitalize() for string in d]
['Blue', 'Sd']
>>> c = ('asd','blue')#元组
>>> c.capitalize()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'capitalize'
>>> [string.capitalize() for string in c]
['Asd', 'Blue']
>>>
>>> a = {'abc':'wsd','ews':'edr'}#字典
>>> a.capitalize()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'dict' object has no attribute 'capitalize'
>>> [string.capitalize() for string in a]
['Abc', 'Ews']
>>>
---------------------
转载,仅作分享,侵删
作者:想搞网络安全
原文:https://blog.csdn.net/weixin_42859280/article/details/84675770
|
|