capitalize is a string method, and c[0:2] is a list, so when you call capitalize Something went wrong. capitalize 是字串的方法,而 c[0:2] 是一個 list,所以你調用 captitalize 的時候會出錯.
c = ['zz','yy','xx']
c = [string.capitalize() for string in c]
還有這樣也可以:
c = ['xx', 'yy', 'zz']
c = ' '.join(c).title().split()
P.S. 在使用 list 的時候,如果要操作的是整個串列,那不需要特別使用到切片,c[0:2] 在這裡是個不必要的做法.
給你參考!
@moling3650, 使用 title 真的是個有趣的主意,capitalize 只會將字串的首字大寫,而 title
>>> string = 'my name is dokelung'
>>> string.capitalize()
'My name is dokelung'
>>> string.title()
'My Name Is Dokelung'
P.S. When using list, if you want to operate the entire list, there is no need to use slicing. c[0:2] is unnecessary here.
For your reference!
@moling3650, Using title is really an interesting idea. capitalize will only capitalize the first word of the string, while title The first character of all #🎜🎜# words #🎜🎜# in the string will be capitalized. #🎜🎜#
#🎜🎜# See example: #🎜🎜#
rrreee
#🎜🎜#So this is okay: #🎜🎜#
rrreee
capitalize
is a string method, andc[0:2]
is a list, so when you callcapitalize
Something went wrong.capitalize
是字串的方法,而c[0:2]
是一個 list,所以你調用captitalize
的時候會出錯.還有這樣也可以:
P.S. 在使用 list 的時候,如果要操作的是整個串列,那不需要特別使用到切片,
c[0:2]
在這裡是個不必要的做法.給你參考!
@moling3650, 使用
You can also do this: P.S. When using list, if you want to operate the entire list, there is no need to use slicing.title
真的是個有趣的主意,capitalize
只會將字串的首字大寫,而title
c[0:2]
is unnecessary here.@moling3650, Using
title
is really an interesting idea.capitalize
will only capitalize the first word of the string, whiletitle
The first character of all #🎜🎜# words #🎜🎜# in the string will be capitalized. #🎜🎜# #🎜🎜# See example: #🎜🎜# rrreee #🎜🎜#So this is okay: #🎜🎜# rrreeeWrite something different
The list object has no capitalize method
Write something different