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

后端 未结 2 408
逝去的感伤
逝去的感伤 2021-01-26 12:54

WHOLE QUESTION: Write a function that takes as a parameter a list of strings and returns a list containing the each string capitalized as a title. That is, if the input paramete

相关标签:
2条回答
  • 2021-01-26 13:28

    You're operating on the list, not a element of the list.

     r.title()
    

    This makes no sense.

    0 讨论(0)
  • 2021-01-26 13:31

    Just iterate over the name list and then for each name, change the case of first letter only by specifying the index number of first letter. And then add the returned result with the remaining chars then finally append the new name to the already created empty list.

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

    OR

    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)
    
    0 讨论(0)
提交回复
热议问题