Python字符串title()
Python String title()
Python String title()
Python字符串title()
Python String title() function returns a title cased version of the string. The first character of the words are in Uppercase and all the remaining characters are in Lowercase.
Python字符串title()函数返回字符串的标题⼤⼩写形式。 单词的第⼀个字符⽤⼤写字母表⽰,其余所有字符⽤⼩写字母表⽰。Python字符串title() (Python String title())
This function doesn’t accept any argument. Let’s look at some examples of title() function.
此函数不接受任何参数。 让我们看⼀些title()函数的例⼦。
s = 'Python is Good!'
print(s.title())
s = 'PYTHON IS GOOD'
print(s.title())
Output:
输出:
Python Is Good!
Python Is Good
This function uses a simple language-independent definition of a word as groups of consecutive letters. So apostrophes (‘) is treated as a word boundary. Let’s see what happens when our string contains an apostrophe.
此函数将单词的简单语⾔独⽴定义⽤作连续字母的组。 因此,撇号(')被视为单词边界。 让我们看看当我们的字符串包含撇号时会发⽣什么。
s = "Let's go to Party"
print(s.title())
Output: Let'S Go To Party
输出: Let'S Go To Party
Most of the times we don’t want this to happen. We can use a regular expression to define a function to convert a string into title cased.
⼤多数时候,我们不希望这种情况发⽣。 我们可以使⽤正则表达式定义⼀个函数,将字符串转换为⼤⼩写的标题。
import re
def titlecase(s):
python正则表达式不包含return re.sub(r"[A-Za-z]+('[A-Za-z]+)?",
lambda mo:
s = "Let's go to Party"
print(titlecase(s))
print(titlecase('Python is Good!'))
print(titlecase('PYTHON IS GOOD'))
Output:
输出:
Let's Go To Party
Python Is Good!
Python Is Good
If you are not familiar with lambda expressions, please read .如果您不熟悉lambda表达式,请阅读 。
.
签出更多Python⽰例。
Official Documentation:
官⽅⽂件:
翻译⾃:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。