how to edit each member of a list in python

后端 未结 2 1845
难免孤独
难免孤独 2021-01-28 04:20

I am new to python and I am trying to create a capitalize function that either capitalizes all words in a string or only the first word. Here is my function

def          


        
2条回答
  •  广开言路
    2021-01-28 04:53

    Use a list comprehension:

    def capitalize(s, applyToAll=False):
        if applyToAll:
            l = [w.capitalize() for w in s.split()]
            return " ".join(l)
        else:
            return s.capitalize()
    

    what do yo guys use in python to debug?

    print statements for complicated pieces of code, the interactive interpreter for anything else. I write a lot of tests through, and run them with nose.

提交回复
热议问题