如何仅将列表中每个字符串的标题大写?

How to capitalize only the title of each string in the list?

整个问题:编写一个函数,将字符串列表作为参数,returns 是一个包含每个大写字符串作为标题的列表。也就是说,如果输入参数是["apple pie", "brownies","chocolate","dulce de leche","eclairs"],你的函数应该return["Apple Pie", "Brownies","Chocolate","Dulce De Leche","Eclairs"]

我的程序(已更新):

我想我已经掌握了我的程序 运行 现在!问题是当我输入:["apple pie"] 它是 returning:['"Apple Pie"']

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()

我遇到错误 AttributeError: 'list' object has no attribute 'title' 请帮忙!

您是在列表中操作,而不是列表中的元素。

 r.title()

这没有意义。

只需遍历名称列表,然后对于每个名称,仅通过指定首字母的索引号来更改首字母的大小写。然后将返回的结果与剩余的字符相加,最后将新名称附加到已经创建的空列表中。

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 r

def main():
  y = Strings()
  x = Capitalize(y)
  print(x)

main()

import re
strings = input("Please enter a list of strings: ")
List = [re.sub(r'^[A-Za-z]|(?<=\s)[A-Za-z]', lambda m: m.group().upper(), name) for name in strings.replace('"','').replace('[','').replace(']','').split(",")]
print(List)