教程集 www.jiaochengji.com
教程集 >  Python编程  >  Python入门  >  正文 Python字符串大小写转换的函数及用法

Python字符串大小写转换的函数及用法

发布时间:2021-12-23   编辑:jiaochengji.com
教程集为您提供Python字符串大小写转换的函数及用法等资源,欢迎您收藏本站,我们将为您提供最新的Python字符串大小写转换的函数及用法资源

Python字符串由内建的str类代表,那么str 类包含哪些方法呢?Python 非常方便,它甚至不需要用户查询文档,Python 是“自带文档”的。

这里需要读者简单掌握两个帮助函数:

dir():列出指定类或模块包含的全部内容(包括函数、方法、类、变量等)。

help():查看某个函数或方法的帮助文档。

例如,要查看 str 类包含的全部内容,可以在交互式解释器中输入如下命令:

<pre class="brush:html;toolbar:false">>>> dir(str) ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__',  '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__',   '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__',   '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__',   '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find',   'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower',    'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip',    'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip',    'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']</pre>

上面列出了 str 类提供的所有方法,其中以“_”开头、“_”结尾的方法被约定成私有方法,不希望被外部直接调用。

如果希望查看某个方法的用法,则可使用 help() 函数。例如,在交互式解释器中输入如下命令:

<pre class="brush:html;toolbar:false">>>> help(str.title) Help on method_descriptor: title(...)     S.title() -> str         Return a titlecased version of S, i.e. words start with title case     characters, all remaining cased characters have lower case.</pre>

从上面介绍可以看出,str 类的 title() 方法的作用是将每个单词的首字母大写,其他字母保持不变。

在 str 类中与大小写相关的常用方法如下:

title():将每个单词的首字母改为大写。

lower():将整个字符串改为小写。

upper():将整个字符串改为大写。

例如,如果希望看到 lower() 方法的相关用法,可运行如下命令:

<pre class="brush:html;toolbar:false">>>> help(str.lower) Help on method_descriptor: lower(...)     S.lower() -> str         Return a copy of the string S converted to lowercase.</pre>

如下代码示范了 str 的与大小写相关的常用方法:

<pre class="brush:html;toolbar:false">a = 'our domain is crazyit.org' # 每个单词首字母大写 print(a.title()) # 字符串中所有字符都小写 print(a.lower()) # 字符串中所有字符都大写 print(a.upper())</pre>

运行上面程序,可以看到如下输出结果:

<pre class="brush:html;toolbar:false">Our Domain Is Crazyit.Org our domain is crazyit.org OUR DOMAIN IS CRAZYIT.ORG</pre>

<span style="color: rgb(68, 68, 68); font-family: "Helvetica Neue", 微软雅黑, "Microsoft Yahei", Helvetica, Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">
</span>

您可能感兴趣的文章:
php大小写转换函数(strtolower、strtoupper)用法简介
php字符串函数有哪些
PHP字符串函数与使用分析
python如何把小写字母变成大写字母
php字符串大小写转换的实现方法
php转换大写的实现方法
php中的大小写转换函数是什么
php5 字符串处理函数汇总
php整理字符串方法
php 字母大小写转换的实例总结

[关闭]
~ ~