About Python capwords

tanby picture tanby · May 21, 2017 · Viewed 9.2k times · Source
from string import capwords

capwords('\"this is test\", please tell me.')
# output: '\"this Is Test\", Please Tell Me.'
             ^

Why is it not equal to this? ↓

'\"This Is Test\", Please Tell Me.'
   ^

How can I do it?

Answer

BoarGules picture BoarGules · May 21, 2017

It doesn't work because it is naive and is confused by the leading " which makes it think that "This does not begin with a letter.

Use the built-in string method .title() instead.

>>> '\"this is test\", please tell me.'.title()
'"This Is Test", Please Tell Me.'

This probably is the reason why capwords() remains in the string module but was never made a string method.