問題:編寫一個函數,它將一個字符串列表作爲參數,並返回一個包含大寫爲標題的每個字符串的列表。也就是說,如果輸入參數是["apple pie","brownies","dulce de leche"]
,則您的函數應返回["Apple Pie","Brownies","Dulce De Leche"]
。如何大寫列表中每個字符串的標題?
我寫了這個程序。問題是如果我輸入["apple pie", "brownies","dulce de leche"]
,則返回['Apple Pie', ' Brownies', 'Dulce De Leche']
如問題所示,每個字符串都必須用逗號打印出來,並且在逗號後面沒有空格。 請幫助!
這裏是我的程序:
def Strings():
s = []
strings = input("Please enter a list of strings: ").title()
List = strings.replace('"',"").replace('[','').replace(']','').split(",")
List = List + s
return List
def Capitalize(parameter):
r = []
for i in parameter:
r.append(i)
return r
def main():
y = Strings()
x = Capitalize(y)
print(x)
main()
這是一個DUP:http://stackoverflow.com/questions/29463718/how-to-capitalize-only-the-title -of-each-string-in-the-list – dbliss 2015-04-06 02:36:13