1.capitalize()  首字母大写

text = "hello word"
text2 = text.capitalize()
print(text2)

2.1.casefold()  字符串小写(可以变成小写的范围比lower大)

text = "Hello Word"
text2 = text.casefold()
print(text2)

2.2.lower()  字符串小写

text = "Hello Word"
text2 = text.lower()
print(text2)

3.center()  设置字符串的宽度,并把内容居中。可以设置其中填充字符的样式,但是只能用一个字符

text = "Hello Word"
text2 = text.center(20,"空")
print(text2)

4.count()  在字符串中寻找子序列出现的次数,可以设置寻找的起始位置和结束位置

text = "Hello Word"
text2 = text.count('l',3,4)
print(text2)

5.endswith()  判断一个字符串是否是以子序列结束,可以设置寻找的起始位置和结束位置

6.startswith()  判断一个字符串是否是以子序列开头,可以设置寻找的起始位置和结束位置

text = "Hello Word"
text2 = text.endswith('rd',8,10)
text3 = text.startswith('rd',8,10)
print(text2)
print(text3)

7.1.find()  从一个字符串头部寻找指定的子序列,如果找到了则返回位置,否则返回-1

text = "Hello Word"
text2 = text.find('o',5,9)
print(text2)

7.2.index()  从一个字符串头部寻找指定的子序列,如果找到了则返回位置,否则报错

text = "Hello Word"
text2 = text.index("H")
print(text2)

8.1.format()  将字符串中的占位符替换成指定的值,如果占位符中为空,按顺序替换

text = "{Hello} {Word}"
text3 = "{}{}"
text2 = text.format(Hello='no',Word='no')
text4 = text3.format('no','no')
print(text2,text4)

8.2.format_map()  像字典一样使用

text = "{Hello} {Word}"
text2 = text.format_map({"Hello":"no","Word":8})
print(text2)

9.1.isalnum()  判断一个字符串是否全是字母数字

text = "HelloWord"
text2 = text.isalnum()
print(text2)

9.2.isalpha()  判断一个字符串是否全是字母,汉字

text = "HelloWord"
text2 = text.isalpha()
print(text2)

10.expandtabs()  断句

text = "Hello\tWord"
text2 = text.expandtabs(20)
print(text2)

11.1.isdecimal()  判断一个字符串是否全是数字

11.2.isdigit()  判断一个字符串是否全是数字,但是数字的范围更大

11.3.isnumeric()  中文数字也可以

text = "②"
text2 = text.isdecimal()
text3 = text.isdigit()
print(text2,text3)

12.isidentifier()  判断字母,数字,下划线,标识符,def,class

text = "a1_def"
text2 = text.isidentifier()
print(text2)

13.isprintable()  判断是否存在转义符。如果没有则返回True

text = "123\t45"
text2 = text.isprintable()
print(text2)

14.isspace()  判断是否全是空格

text = "  "
text2 = text.isspace()
print(text2)

15.1.istitle()  判断每个单词第一个都是大写

text = "Hello Word"
text2 = text.istitle()
print(text2)

15.2.title()  把字符串转换成标题

text = "hello word"
text2 = text.title()
print(text2)

16.join()  在字符串每一个字符之间加上子序列

text = "hello word"
text2 = "*".join(text)
print(text2)

17.1.ljust()  设置一个字符串的宽度,如果长度不够则在右边加上指定的字符

17.2.rjust()  设置一个字符串的宽度,如果长度不够则在左边加上指定的字符

text = "hello word"
text2 = text.ljust(20,"")
text3 = text.rjust(20,"")
print(text2,text3)

18.zfill()  设置一个字符串的宽度,如果长度不够则在左边加上0  

text = "hello word"
text2 = text.zfill(20)
print(text2)

19.1.lower()  把一个字符串全部变成小写

19.2.islower()  判断一个字符串是否全是小写

text = "hello word"
text2 = text.lower()
text3 = text.islower()
print(text2,text3)

20.1.upper()  把一个字符串全部变成大写

20.2.isupper()  判断一个字符串是否全是大写

text = "hello word"
text2 = text.upper()
text3 = text.isupper()
print(text2,text3)

21.1.strip()  去掉字符串两侧的空格

21.2.lstrip()  去掉字符串左侧的空格

21.3.rstrip()  去掉字符串右侧的空格

text = " hello word "
text2 = text.lstrip()
text3 = text.rstrip()
text4 = text.strip()
print(text2)
print(text3)
print(text4)

22.1.partition()  分割出第一个子序列

text = " hello word "
text2 = text.partition('e')
print(text2)

22.2.rpartition()  从右侧分割出第一个子序列

text = " hello woerd "
text2 = text.rpartition('e')
print(text2)

22.3.split()  分割出全部子序列,可以设置分割的次数

22.4.rsplit()  从右侧分割出全部子序列,可以设置分割的次数

text = " hello woerd "
text2 = text.split('e',2)
print(text2)

22.5.splitlines()  分割换行,可以选择是否隐藏

text = " hello\nwoerd "
text2 = text.splitlines(True)
print(text2)

23.1.startswith()  判断一个字符串是否以子序列开头

text = " hello woerd "
text2 = text.startswith(' ')
print(text2)

24.swapcase()  大小写转换

text = " hello woerd "
text2 = text.swapcase()
print(text2)

python,字符串方法的更多相关文章

  1. python字符串方法的简单使用

    学习python字符串方法的使用,对书中列举的每种方法都做一个试用,将结果记录,方便以后查询. (1) s.capitalize() ;功能:返回字符串的的副本,并将首字母大写.使用如下: >& ...

  2. Python 字符串方法详解

    Python 字符串方法详解 本文最初发表于赖勇浩(恋花蝶)的博客(http://blog.csdn.net/lanphaday),如蒙转载,敬请保留全文完整,切勿去除本声明和作者信息.        ...

  3. python 字符串方法整理

    Python字符串方法 1.大小写转换 1.1 lower.upper lower():小写 upper():大写 1.2 title.capitalize S.title():字符串中所有单词首字母 ...

  4. python 字符串方法isdigit()

    python isdigit() 方法检测字符串是否只有数字组成. 语法: isdigit()方法语法: str.isdigit() 参数:无 返回值: 如果字符串中只含有数字则返回True,否则返回 ...

  5. python字符串方法以及注释

    转自fishC论坛:http://bbs.fishc.com/forum.php?mod=viewthread&tid=38992&extra=page%3D1%26filter%3D ...

  6. python字符串方法replace()简介

    今天写replace方法的时候的代码如下: message = "I really like dogs" message.replace('dog','cat') print(me ...

  7. [python]字符串方法

    字符串的方法及注释 字符串的方法及注释             capitalize()   把字符串的第一个字符改为大写   casefold()   把整个字符串的所有字符改为小写   cente ...

  8. Python字符串方法

    capitalize() 把字符串的第一个字符改为大写 casefold() 把整个字符串的所有字符改为小写 center(width) 将字符串居中,并使用空格填充至长度 width 的新字符串 c ...

  9. Python字符串方法总结(一)

    1.find 在一个较长的字符串中查找子串.它返回子串所在位置的最左端索引.如果没有找到则返回-1 2.split 将字符串用给定的分隔符分割成序列,当没有提供分隔符时,默认把所有空格作为分隔符 3. ...

  10. python 字符串方法及列表,元组,字典(一)

    字符串 str 注: 若想要保持单引号和双引号为字符串的一部分 1)单双引号交替使用, 2)使用转义字符\ 3)成对三个引号被存在变量里 二.字符串详细用法 字符串的单个取值例 p_1=”hello” ...

随机推荐

  1. Ubuntu如何百度云盘下载

    我使用Firefox浏览器下载. (1)先为浏览器下载一个插件:网盘助手 (2)通过终端安装aria2: sudo apt-get install python-apt sudo apt-get in ...

  2. struts1和struts2和springMVC的区别和介绍

    MVC是web开发常用的模式,M即模型层(Model):主要由javabean来实现.V即视图层(View):主要由jsp.velocity.freemarker等.C即控制层(Controller) ...

  3. 三、后门的编写和 ShellCode 的提取

    第三章.后门的编写和 ShellCode 的提取 (一)IP 和 Socket 编程初步 NOTES: 1.Windows 下网络通信编程的几种方式 第一种是基于 NetBIOS 的网络编程,这种方法 ...

  4. python接口测试-将运行结果写入Excel表格

    公司工作是促进学习的第一生产力!! 一个get请求的接口,我想清楚的在Excel中看到所有的数据! 带着学过H5,php觉得所有代码都很简单的自信,在公司开发的[鼓励]下开始了一上午的斗争 一个小时. ...

  5. Android版本28使用http请求

    Android版本28使用http请求报错not permitted by network security policy android模拟器调试登录的时候报错 CLEARTEXT communic ...

  6. Windows与Linux的命令行命令对比

    Windows与Linux的命令行命令对比 * Windows不区分大小写,Linux区分大小写的. sn DOS Command UNIX Equivalent Effect 影响 1 ASSIGN ...

  7. C++动态链接库方法调用

    //定义内存的信息结构 [StructLayout(LayoutKind.Sequential)] public struct MEMORY_INFO { public uint dwLength; ...

  8. 使用C#的aforge类库识别验证码实例

    一: 验证码处理 1.  一般处理原则 这种验证码为什么说跟没有一样,第一点:字体规范工整,第二点:不旋转扭曲粘连,第三点:字体颜色单一,下面看处理步骤. 这里要注意的是,aforge只接受像素格式为 ...

  9. MySQL 读写分离(转载)

    原文地址:https://blog.csdn.net/justdb/article/details/17331569

  10. JavaScript中的ononline事件和onoffline事件

    关于这个时间的描述到处都有,但基本上都是说离线在线什么的我一下子还没反应过来.后再在这里看到了一句话:"断开网络再联网试试,就可以看到连线的提示."这才反应过来,原来指的是网络状态 ...