Titlecasing a string with exceptions

前端 未结 9 1629
栀梦
栀梦 2020-11-28 06:28

Is there a standard way in Python to titlecase a string (i.e. words start with uppercase characters, all remaining cased characters have lowercase) but leaving articles like

9条回答
  •  隐瞒了意图╮
    2020-11-28 07:09

    There are these methods:

    >>> mytext = u'i am a foobar bazbar'
    >>> print mytext.capitalize()
    I am a foobar bazbar
    >>> print mytext.title()
    I Am A Foobar Bazbar
    

    There's no lowercase article option. You'd have to code that yourself, probably by using a list of articles you want to lower.

提交回复
热议问题