如何在Python中将字符串转换为标题大小写?
是否有库或标准方法来执行此任务?
例子:
HILO -> Hilo
new york -> New York
SAN FRANCISCO -> San Francisco
Is there a library or standard way to perform this task?
Example:
HILO -> Hilo
new york -> New York
SAN FRANCISCO -> San Francisco
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
为什么不直接从文档中使用
title
:如果你真的想要 PascalCase 你可以使用这个:
Why not use
title
Right from the docs:If you really wanted PascalCase you can use this:
这个总是以小写字母开头,并且还会删除非字母数字字符:
This one would always start with lowercase, and also strip non alphanumeric characters:
re.sub
可以采用一个函数来进行“替换”(而不仅仅是一个字符串,这是大多数人似乎熟悉的用法)。对于模式的每个匹配,将使用re.Match
对象调用此 repl 函数,并且结果(应该是字符串)将用作该匹配的替换。同一事物的更长版本:
这会预编译模式(通常被认为是良好的形式)并使用命名函数而不是 lambda。
re.sub
can take a function for the "replacement" (rather than just a string, which is the usage most people seem to be familiar with). This repl function will be called with anre.Match
object for each match of the pattern, and the result (which should be a string) will be used as a replacement for that match.A longer version of the same thing:
This pre-compiles the pattern (generally considered good form) and uses a named function instead of a lambda.
潜在的库: https://pypi.org/project/stringcase/
示例:
虽然它是否是值得怀疑的会留下空格。(示例显示它删除了空格,但有一个错误跟踪器问题指出它留下了空格。)
Potential library: https://pypi.org/project/stringcase/
Example:
Though it's questionable whether it will leave spaces in. (Examples show it removing space, but there is a bug tracker issue noting that it leaves them in.)
只需使用 .title() ,它就会将每个单词的第一个字母转换为大写,其余为小写:
just use .title(), and it will convert first letter of every word in capital, rest in small:
为什么不写一个呢?像这样的东西可能会满足您的要求:
Why not write one? Something like this may satisfy your requirements:
注意:为什么我要提供另一个答案? 这个答案基于问题的标题和驼峰命名的概念:一系列已连接的单词(没有空格) !)使得每个原始单词都以大写字母开头(其余为小写),除了该系列的第一个单词(完全小写)。还假设“所有字符串”指的是 ASCII 字符集; unicode 不适用于此解决方案)。
simple
鉴于上述定义,
调用此函数时,会导致这种方式
不太简单。
请注意,当呈现已驼峰式大小写的字符串时,它会失败!
甚至更不简单
请注意,它会因许多 unicode 字符串而失败,
我没有针对这些情况的解决方案(或可以通过一些创造力引入的其他解决方案)。因此,就像所有与字符串有关的事情一样,请涵盖您自己的边缘情况,祝您使用 unicode 好运!
Note: Why am I providing yet another answer? This answer is based on the title of the question and the notion that camelcase is defined as: a series of words that have been concatenated (no spaces!) such that each of the original words start with a capital letter (the rest being lowercase) excepting the first word of the series (which is completely lowercase). Also it is assumed that "all strings" refers to ASCII character set; unicode would not work with this solution).
simple
Given the above definition, this function
, when called, would result in this manner
less simple
Note that it fails when presented with an already camel cased string!
even less simple
Note that it fails with many unicode strings
I don't have a solution for these cases(or other ones that could be introduced with some creativity). So, as in all things that have to do with strings, cover your own edge cases and good luck with unicode!
我想为这篇文章添加我的一点贡献:
I would like to add my little contribution to this post:
来自代码战争 - 在 Python 中为字符串编写简单的 .camelCase 方法。所有单词的第一个字母必须大写,不带空格。
驼峰(“你好”)=>哈罗案例
camelcase("驼峰式单词") =>驼峰式单词
From code wars - Write simple .camelCase method in Python for strings. All words must have their first letter capitalized without spaces.
camelcase("hello case") => HelloCase
camelcase("camel case word") => CamelCaseWord
这是一个例子:
Here is an example: