Python title() with apostrophes

后端 未结 5 2020
不思量自难忘°
不思量自难忘° 2020-12-16 09:06

Is there a way to use .title() to get the correct output from a title with apostrophes? For example:

\"john\'s school\".title() --> \"John\'S         


        
5条回答
  •  [愿得一人]
    2020-12-16 09:46

    If your titles do not contain several whitespace characters in a row (which would be collapsed), you can use string.capwords() instead:

    >>> import string
    >>> string.capwords("john's school")
    "John's School"
    

    EDIT: As Chris Morgan rightfully says below, you can alleviate the whitespace collapsing issue by specifying " " in the sep argument:

    >>> string.capwords("john's    school", " ")
    "John's    School"
    

提交回复
热议问题