Python title() 带撇号 [英] Python title() with apostrophes

查看:27
本文介绍了Python title() 带撇号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用 .title() 从带有撇号的标题中获得正确的输出?例如:

"john's school".title() -->约翰学校"

我如何在这里获得正确的标题,John's School"?

解决方案

如果您的标题在一行中没有包含多个空格字符(这会被折叠),您可以使用 string.capwords() 代替:

<预><代码>>>>导入字符串>>>string.capwords("约翰的学校")约翰学校"

正如 Chris Morgan 在下面正确地说的,您可以通过在 sep 参数中指定 " " 来缓解空白折叠问题:

<预><代码>>>>string.capwords("约翰的学校", " ")约翰学校"

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 School"

How would I get the correct title here, "John's School" ?

解决方案

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"

这篇关于Python title() 带撇号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆