博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python字符串title()
阅读量:2533 次
发布时间:2019-05-11

本文共 1614 字,大约阅读时间需要 5 分钟。

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 redef titlecase(s):    return re.sub(r"[A-Za-z]+('[A-Za-z]+)?",                  lambda mo:                  mo.group(0)[0].upper() +                  mo.group(0)[1:].lower(), s)s = "Let's go to Party"print(titlecase(s))print(titlecase('Python is Good!'))print(titlecase('PYTHON IS GOOD'))

Output:

输出:

Let's Go To PartyPython Is Good!Python Is Good

If you are not familiar with lambda expressions, please read .

如果您不熟悉lambda表达式,请阅读 。

. 签出更多Python示例。

Official Documentation:

官方文件:

翻译自:

转载地址:http://glmzd.baihongyu.com/

你可能感兴趣的文章
STM32+IAP方案 实现网络升级应用固件
查看>>
用74HC165读8个按键状态
查看>>
jpg转bmp(使用libjpeg)
查看>>
linear-gradient常用实现效果
查看>>
sql语言的一大类 DML 数据的操纵语言
查看>>
VMware黑屏解决方法
查看>>
JS中各种跳转解析
查看>>
JAVA 基础 / 第八课:面向对象 / JAVA类的方法与实例方法
查看>>
Ecust OJ
查看>>
P3384 【模板】树链剖分
查看>>
Thrift源码分析(二)-- 协议和编解码
查看>>
考勤系统之计算工作小时数
查看>>
4.1 分解条件式
查看>>
Equivalent Strings
查看>>
flume handler
查看>>
收藏其他博客园主写的代码,学习加自用。先表示感谢!!!
查看>>
H5 表单标签
查看>>
su 与 su - 区别
查看>>
C语言编程-9_4 字符统计
查看>>
在webconfig中写好连接后,在程序中如何调用?
查看>>