如何将列表中每个字符串的标题大写?
How to capitalize the title of each string in a list?
问题:编写一个函数,将字符串列表作为参数,returns 是一个包含每个大写字符串作为标题的列表。也就是说,如果输入参数是 ["apple pie","brownies","dulce de leche"]
,你的函数应该 return ["Apple Pie","Brownies","Dulce De Leche"]
.
这个程序是我写的。问题是如果我输入 ["apple pie", "brownies","dulce de leche"]
,它是 returning ['Apple Pie', ' Brownies', 'Dulce De Leche']
正如问题所暗示的,每个字符串都必须用引号打印出来,逗号后没有 space 。
请帮忙!
这是我的程序:
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()
不确定为什么会有人想要这样做,但你可以:
s = "["
for i in x:
s += '"' + i + '",'
print s + "]"
和我之前的回答一样,你只需要用","
加入列表,你需要在开始和结束时添加["
,"]
。
def Strings():
strings = input("Please enter a list of strings: ")
List = strings.replace('"','').replace('[','').replace(']','').split(",")
return List
def Capitalize(parameter):
r = []
for i in parameter:
m = ""
for j in i.split():
m += j[0].upper() + j[1:] + " "
r.append(m.rstrip())
return '["' + '","'.join(r)+ '"]'
def main():
y = Strings()
x = Capitalize(y)
print(x)
main()
def Capitalize(parameter):
return ["\""+i.strip().strip("\"").title()+"\"" for i in s[1:-1].split(",")]
title = Capitalize(y)
print "[" + ",".join(title) + "]"
如果列表作为合法 python 列表输入 [...] 那么您可以使用 json 模块将此字符串转换为 python object (即列表),然后在给字符串加标题后再次返回,json.dumps()
让您可以控制分隔符,例如:
>>> from json import loads, dumps
>>> strings = loads(input("Please enter a list of strings: "))
>>> print(dumps([s.title() for s in strings], separators=(',',':')))
["Apple Pie","Brownies","Dulce De Leche"]
问题:编写一个函数,将字符串列表作为参数,returns 是一个包含每个大写字符串作为标题的列表。也就是说,如果输入参数是 ["apple pie","brownies","dulce de leche"]
,你的函数应该 return ["Apple Pie","Brownies","Dulce De Leche"]
.
这个程序是我写的。问题是如果我输入 ["apple pie", "brownies","dulce de leche"]
,它是 returning ['Apple Pie', ' Brownies', 'Dulce De Leche']
正如问题所暗示的,每个字符串都必须用引号打印出来,逗号后没有 space 。
请帮忙!
这是我的程序:
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()
不确定为什么会有人想要这样做,但你可以:
s = "["
for i in x:
s += '"' + i + '",'
print s + "]"
和我之前的回答一样,你只需要用","
加入列表,你需要在开始和结束时添加["
,"]
。
def Strings():
strings = input("Please enter a list of strings: ")
List = strings.replace('"','').replace('[','').replace(']','').split(",")
return List
def Capitalize(parameter):
r = []
for i in parameter:
m = ""
for j in i.split():
m += j[0].upper() + j[1:] + " "
r.append(m.rstrip())
return '["' + '","'.join(r)+ '"]'
def main():
y = Strings()
x = Capitalize(y)
print(x)
main()
def Capitalize(parameter):
return ["\""+i.strip().strip("\"").title()+"\"" for i in s[1:-1].split(",")]
title = Capitalize(y)
print "[" + ",".join(title) + "]"
如果列表作为合法 python 列表输入 [...] 那么您可以使用 json 模块将此字符串转换为 python object (即列表),然后在给字符串加标题后再次返回,json.dumps()
让您可以控制分隔符,例如:
>>> from json import loads, dumps
>>> strings = loads(input("Please enter a list of strings: "))
>>> print(dumps([s.title() for s in strings], separators=(',',':')))
["Apple Pie","Brownies","Dulce De Leche"]