Capitalize a string

前端 未结 9 736
轻奢々
轻奢々 2020-11-27 19:33

Does anyone know of a really simple way of capitalizing just the first letter of a string, regardless of the capitalization of the rest of the string?

For example:

相关标签:
9条回答
  • 2020-11-27 20:05

    this actually gives you a capitalized word, instead of just capitalizing the first letter

    cApItAlIzE -> Capitalize

    def capitalize(str): 
        return str[:1].upper() + str[1:].lower().......
    
    0 讨论(0)
  • 2020-11-27 20:08
    >>> b = "my name"
    >>> b.capitalize()
    'My name'
    >>> b.title()
    'My Name'
    
    0 讨论(0)
  • 2020-11-27 20:08

    What about your_string.title()?

    e.g. "banana".title() -> Banana

    0 讨论(0)
提交回复
热议问题