美文网首页
python字符串

python字符串

作者: 86dc3d7f2eae | 来源:发表于2018-06-14 23:47 被阅读0次

一.检测 str 是否包含在 mystr中,如果是返回开始的索引值,否则返回-1

In [1]: str = 'hello wo shi yang ya ming'
In [2]: str
Out[2]: 'hello wo shi yang ya ming'
In [3]: str.index('wo')
Out[3]: 6
In [4]: str.index('hjk')

二、count
计算字符串str中出现某个字符的次数(这里为c和b),如果字符串中没有出现返回 "0"

In [7]: str
Out[7]: 'hello wo shi zi fu chuan jiu shi wo'
In [8]: str.count('a')
Out[8]: 1
In [9]: str.count('b')
Out[9]: 0

三、replace
替换字符串str中的字符(比如用‘z’替换‘str’中的'c' -全部;用'a'替换'o',从索引开始数替换-1次),可以指定替换几处,默认全部替换

In [10]: str
Out[10]: 'hello wo shi zi fu chuan jiu shi wo'
In [11]: str.replace('d','b')
Out[11]: 'hello wo shi zi fu chuan jiu shi wo'
In [12]: 'hello wo shi zi fu chuan jiu shi wo'
Out[12]: 'hello wo shi zi fu chuan jiu shi wo'
In [15]: str.replace('o','a',1)
Out[15]: 'hella wo shi zi fu chuan jiu shi wo'

四、split
以 str 为分隔符切片 ,分隔 出多个子字符串,可以设定最大值

In [16]: str
Out[16]: 'hello wo shi zi fu chuan jiu shi wo'
In [17]: str.split(' ')
Out[17]: ['hello', 'wo', 'shi', 'zi', 'fu', 'chuan', 'jiu', 'shi', 'wo']
In [18]: str.split(' ',2)
Out[18]: ['hello', 'wo', 'shi zi fu chuan jiu shi wo']
In [19]: ['wo', 'shi', 'zi' 'fu']
Out[19]: ['wo', 'shi', 'zifu']
In [20]: str
Out[20]: 'hello wo shi zi fu chuan jiu shi wo'

五、startswith
检查字符串是否是以'z'开头,如果是,返回''True'',不是就返回''False''

In [20]: str
Out[20]: 'hello wo shi zi fu chuan jiu shi wo'
In [21]: str
Out[21]: 'hello wo shi zi fu chuan jiu shi wo'
In [22]: str.startswith('z')
Out[22]: False
In [23]: str.startswith('i')
Out[23]: False

六、endswith
和startswith相反,检查字符串是否是以‘o’结尾,是就返回True ,不是就返回False

In [24]: str
Out[24]: 'hello wo shi zi fu chuan jiu shi wo'
In [25]: str.endswith('o')

七、lstrip
删除字符串''str'' 左边的空白字符,不改变原文件

In [32]: str
Out[32]: '      zi fu zhuan zhang jian cao zuo      '
In [33]: ' str zhun zhang jian cao zuo    '
Out[33]: ' str zhun zhang jian cao zuo    '
In [34]: str.lstrip()
Out[34]: 'zi fu zhuan zhang jian cao zuo      '

八、strip
删除字符串''str''两端的空白
字符,不改变原文件In [38]:

str
Out[38]: '      zi fu zhuan zhang jian cao zuo      '
In [39]: str.strip()
Out[39]: 'zi fu zhuan zhang jian cao zuo'
In [40]: str
Out[40]: '      zi fu zhuan zhang jian cao zuo      '

九、isdigit
检查字符串''str''中是否只包含数字,如果只包含数字则返回 True 否则返回 False.

In [41]: str1 = '123123'
In [42]: str2 = '2222121'
In [43]: str1.isdigit()
Out[43]: True

十、join
将一个字符串(str1、str3)当作一个字符插入到另一个字符串中的每一个字符后,造出一个新的字符串,不改变原文件

In [52]: str1 = '11111'
In [53]: str2 = '222222'
In [54]: str1.join(str2)
Out[54]: '2111112111112111112111112111112'
In [55]: str = 0
In [56]: str = '0'
In [57]: str3.join(str1)
Out[57]: '101010101'

相关文章

  • python基础知识(3)

    python字符串 python转义字符 python字符串运算符 python字符串格式化 python格式化操...

  • python count()方法详解

    Python count()方法 Python 字符串 描述 Python count() 方法用于统计字符串里某...

  • python字符串格式化符号与内建函数资料表

    python字符串格式化符号: Python 的字符串内建函数 Python 的字符串常用内建函数如下:

  • 字符串操作方法

    Python3字符串 Python访问字符串中的值 Python中的字符串用单引号(')或双引号(")括起来,同时...

  • python字符串相关函数

    Python3字符串 Python访问字符串中的值 Python中的字符串用单引号(')或双引号(")括起来,同时...

  • 2018-09-28自学习资料

    Python3字符串 Python访问字符串中的值 Python中的字符串用单引号(')或双引号(")括起来,同时...

  • 字符串内置函数

    Python3字符串 Python访问字符串中的值 Python中的字符串用单引号(')或双引号(")括起来,同时...

  • 2018-07-18 字符串资料(函数方法)

    Python3字符串资料 Python访问字符串中的值 Python中的字符串用单引号(')或双引号(")括起来,...

  • day4 字符串自主操作

    Python3字符串 Python访问字符串中的值 Python中的字符串用单引号(')或双引号(")括起来,同时...

  • Day3-整理

    Python3字符串 Python访问字符串中的值 Python中的字符串用单引号(')或双引号(")括起来,同时...

网友评论

      本文标题:python字符串

      本文链接:https://www.haomeiwen.com/subject/tfvueftx.html