How can I capitalize the first letter of each word in a string?

前端 未结 19 2275
深忆病人
深忆病人 2020-11-22 17:17
s = \'the brown fox\'

...do something here...

s should be:

\'The Brown Fox\'

What\'s the easiest

19条回答
  •  [愿得一人]
    2020-11-22 17:28

    An empty string will raise an error if you access [1:]. Therefore I would use:

    def my_uppercase(title):
        if not title:
           return ''
        return title[0].upper() + title[1:]
    

    to uppercase the first letter only.

提交回复
热议问题