【发布时间】:2017-10-21 00:56:33
【问题描述】:
from string import capwords
capwords('\"this is test\", please tell me.')
# output: '\"this Is Test\", Please Tell Me.'
^
为什么不等于这个? ↓
'\"This Is Test\", Please Tell Me.'
^
我该怎么做?
【问题讨论】:
-
string模块是 Python 1 的遗留物,在 Python 2.0 引入字符串方法时几乎完全过时。你几乎从不需要import string。我只能想到两个例外:maketrans()(我不时使用)和依赖于语言环境的大写/小写内容(我从未使用过)。 -
谢谢大家。使用
.title()解决了这个问题。 -
string模块没有什么过时的。一些函数在 Python 2 中被弃用,取而代之的是str上的方法。这些在 Python 3 中消失了,因此使用string模块与这些无关。请参阅文档:docs.python.org/3/library/string.html
标签: python string capitalize