码迷,mamicode.com
首页 > 其他好文 > 详细

05-03字符串与其常用操作

时间:2020-05-28 23:58:02      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:检查   path   索引   password   rom   移动   lin   相等   containe   

目录

字符串及其操作

字符串是集合类型, 并不是一个基本类型

字符串对象操作

新建字符串

  • 方法1:s = ‘hello python‘
  • 方法2:s = "hello python"
  • 方法3:s = ‘‘‘hello python‘‘‘
  • 方法4:s = """hello python"""

前两种完全一样, 后两种完全一样
三引号可以定义多行字符串,单/双引号只能定义单行字符串

方法3或方法4与方法1或方法2的区别:
    In [42]: s = ‘‘‘hello python
        ...: i like python‘‘‘

    In [43]: s = ‘hello python
      File "<ipython-input-43-07b7eed529d4>", line 1
        s = ‘hello python
                         ^
    SyntaxError: EOL while scanning string literal

    三引号可以定义多行字符串
    单/双引号只能定义单行字符串

删除字符串对象

需要使用del方法删除
截至2020年5月23日,本人暂时还未发现其他的方法

字符串元素操作

转义

转义字符(这里的不全,可以互联网搜索)

  • \n:换行
  • \t:制表符
  • \r:回车

回车与换行区别:老式打印机

换行是纸带往上移动一行。
回车是把针头回到开始位置。
window是\n\r换行。unix是\n。

python3中加r前缀代表此字符串是自然字符串

In [47]: s = ‘i like \npython‘

In [48]: print(s)
i like 
python

In [49]: s = ‘i\‘m comyn‘    #‘有特殊作用,所以需要\‘转义一下

In [50]: print(s)
i‘m comyn

三引号常用于:
sql = ‘‘‘select * from `table` where name=‘comyn‘ ‘‘‘


取消转义场景
In [52]: path = ‘c:\windows\nt\system32‘    # 正常情况显示

In [53]: print(path)
c:\windows
t\system32

In [54]: path = ‘c:\windows\\nt\system32‘    # 取消转义

In [55]: print(path)
c:\windows\nt\system32

In [56]: path = ‘c:\\windows\\system32‘   # 也可以将所有的都转移,以防万一。

In [57]: path = r‘c:\windows\nt\system32‘   # 加r前缀代表此字符串是自然字符串, 不会转义,python3中只有\r前缀。python2中有\u前缀。

In [58]: print(path)
c:\windows\nt\system32

In [59]: ‘\n‘ == ‘\r‘    # \n和\r是不相等的。
Out[59]: False

In [60]: print("123\r45")    # \r回到起点,将12盖掉了
453
python3中所有字符串都是unixcode

查询操作

下标操作

  • 字符串也支持下表操作
  • 字符串是无法通过下标更改的
  • 字符串是可迭代对象,也就是说可以作为for循环的值
In [61]: s 
Out[61]: "i‘m comyn"

In [62]: s[3]
Out[62]: ‘ ‘

In [63]: s[4]
Out[63]: ‘c‘

In [64]: type(s[4])
Out[64]: str

In [65]: s[4] = C    # 字符串是不可变的
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-65-a888c3296699> in <module>()
----> 1 s[4] = C

NameError: name ‘C‘ is not defined

In [66]: s
Out[66]: "i‘m comyn"

字符串是可迭代对象:
    In [67]: for c in s:
        ...:     print(c)
        ...:
    i
    ‘
    m

    c
    o
    m
    y
    n

字符串的操作

连接

str.join连接,将可迭代对象转换为字符串
注意:此操作会返回一个新的字符串对象,但不会操作原有可迭代对象

In [73]: lst = [‘i‘, ‘am‘, ‘comyn‘]

In [74]: ‘ ‘.join(lst)   # join是字符串的方法,参数是可迭代对象,接收者是分隔符
Out[74]: ‘i am comyn‘

In [76]: ‘,‘.join(lst)
Out[76]: ‘i,am,comyn‘

In [77]: ‘‘.join(lst)
Out[77]: ‘iamcomyn‘

分割

分割:指定分隔符对字符串进行切片

split通过指定分隔符对字符串进行切片,返回一个list
  • split默认使用空格分割
  • 多个空格当成一个空格处理
  • 当指定了以空格为分割符时,每个空格都处理
  • s.split(maxsplit=1) # 只分割一次,maxsplit参数表示分割多少次,默认是-1, 表示分割所有分隔符
  • s.split(‘lo‘) # 分隔符可以是任意字符串
In [78]: s = ‘i love python‘

In [80]: s.split()    # split默认使用空格分割
Out[80]: [‘i‘, ‘love‘, ‘python‘]

In [81]: ‘i     love python‘.split()   # 多个空格当成一个空格处理
Out[81]: [‘i‘, ‘love‘, ‘python‘]

In [82]: ‘i     love python‘.split(‘ ‘)   # 当指定了以空格为分割符时,每个空格都处理
Out[82]: [‘i‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘love‘, ‘python‘]

In [83]: s.split(maxsplit=1)   # 只分割一次
Out[83]: [‘i‘, ‘love python‘]

In [84]: ‘i i i i i i i i i‘.split(maxsplit=2)   # split从左往右分割字符串, maxsplit参数表示分割多少次, 默认为-1, 表示分割所有分隔符。
Out[84]: [‘i‘, ‘i‘, ‘i i i i i i i‘]

In [85]: s.split(‘o‘)   # 指定o为分割符
Out[85]: [‘i l‘, ‘ve pyth‘, ‘n‘]

In [86]: s.split(‘lo‘)   # 分隔符可以是任意字符串
Out[86]: [‘i ‘, ‘ve python‘]


split的实现:
In [90]: def split(s, sep, maxsplit):
    ...:     ret = []
    ...:     tmp = []
    ...:     i = ()
    ...:     for c in s:
    ...:         if c != sep:
    ...:             tmp.append(c)
    ...:         else:
    ...:             i += 1
    ...:             ret.append(‘‘.join(tmp))
    ...:             tmp.clear()
    ...:         if maxsplit > 0 and i >= maxsplit:
    ...:             ret.append()
    ...:             return ret
    ...:     return ret
rsplit

rsplit:是split从右往左的版本

In [87]: ‘i i i i ii i i‘.rsplit(maxsplit=1)
Out[87]: [‘i i i i ii i‘, ‘i‘]

In [88]: ‘i i i i ii i i‘.rsplit()
Out[88]: [‘i‘, ‘i‘, ‘i‘, ‘i‘, ‘ii‘, ‘i‘, ‘i‘]

In [89]: ‘i i i i ii i i‘.split()
Out[89]: [‘i‘, ‘i‘, ‘i‘, ‘i‘, ‘ii‘, ‘i‘, ‘i‘]
当不是用maxsplit参数时, split和rsplit表现形式一样,但是split效率高于rsplit


rsplit的实现:
In [91]: def rsplit(s, sep, maxsplit):
    ...:     ret = []
    ...:     tmp = []
    ...:     i = ()
    ...:     for c in reversed(s):
    ...:         if c != sep:
    ...:             tmp.append(c)
    ...:         else:
    ...:             i += 1
    ...:             ret.append(‘‘.join(reversed(tmp)))
    ...:             tmp.clear()
    ...:         if maxsplit > 0 and i >= maxsplit:
    ...:             ret.append()
    ...:             return reversed(ret)
    ...:     return reversed(ret)
splitlines:分割行
  • 返回结果不会带换行符
In [92]: s = ‘‘‘ i am comyn
    ...: i love python‘‘‘

In [93]: s.splitlines()   # 按行分割,并且返回结果不带换行符
Out[93]: [‘ i am comyn‘, ‘i love python‘]

In [94]: s.splitlines(True)   # 按行分割, 并且返回结果带换行符
Out[94]: [‘ i am comyn\n‘, ‘i love python‘]
partition返回三元组
  • 当要分割的内容为空时,返回也为空
  • 当要分割的内容为要返回的内容时, 除了sep, 其他都为空
In [95]: ‘i am comyn‘.partition(‘ ‘)   # 总是返回一个三元组, 它按传入分割符分割一次,得到 head tail, 返回结果是head, sep, tail。(sep是指分隔符)
Out[95]: (‘i‘, ‘ ‘, ‘am comyn‘)


partition的实现:
In [103]: def partition(s, sep):
     ...:     if s == ‘‘:
     ...:         return ‘‘,‘‘,‘‘
     ...:     tmp = s.split(sep, mapsplit=1)
     ...:     if len(tmp) == 2:
     ...:         return tmp[0], sep, tmp[1]
     ...:     if len(tmp) == 1:
     ...:         return tmp[0], sep, ‘‘
     ...:     if len(tmp) == 0:
     ...:         return ‘‘, sep, ‘‘
rpartition

rpartition:是partition从右往左的版本

In [96]: cfg = ‘mysql.connect = mysql://user:password@127.0.0.1:3306/test‘

In [97]: cfg.partition(‘=‘)
Out[97]: (‘mysql.connect ‘, ‘=‘, ‘ mysql://user:password@127.0.0.1:3306/test‘)

In [98]: cfg = ‘env = PATH=/usr/bin:$PATH‘

In [99]: cfg.partition(‘=‘)
Out[99]: (‘env ‘, ‘=‘, ‘ PATH=/usr/bin:$PATH‘)

In [100]: ‘‘.partition(‘=‘)   # 当要分割的内容为空时,返回也为空
Out[100]: (‘‘, ‘‘, ‘‘)

In [101]: ‘=‘.partition(‘=‘)   # 当要分割的内容为要返回的内容时, 除了sep, 其他都为空
Out[101]: (‘‘, ‘=‘, ‘‘)

转换

经常用在英文书写格式上面,比如首字母大写。

In [104]: s = ‘test‘

In [105]: s.upper()   # 全部转换为大写
Out[105]: ‘TEST‘

In [106]: s.upper().lower()   # 先转换为大写,然后转换为小写
Out[106]: ‘test‘

In [107]: s.title()   # 首字母转换为大写
Out[107]: ‘Test‘

In [108]: s.capitalize()   # 首字母转换为大写
Out[108]: ‘Test

In [109]: ‘i love python‘.title()   # 与capitalize的区别, title常用于标题
Out[109]: ‘I Love Python‘

In [110]: ‘i love python‘.capitalize()   # 与title的区别, 常用于正文
Out[110]: ‘I love python‘

In [111]: s.center(80)    # 标题居中
Out[111]: ‘                                      test                                      ‘

In [112]: s.zfill(80)    # 居右,用0填充
Out[112]: ‘0000000000000000000000000000000000000000000000000000000000000000000000000000test‘

In [117]: s.casefold()   # 原地返回
Out[117]: ‘test‘

In [118]: ‘Test TTset‘.casefold()   # 使用大小写不同的测试, 全转换为大写或小写, 不同的平台有不同的表现形式, 但是同一个平台下, 表现形式相同, 通常用来忽略大小写的比较。忽略大小写。
Out[118]: ‘test ttset‘ 

In [119]: s.swapcase()   # 大小写互换
Out[119]: ‘TEST‘

In [120]: ‘Test‘.swapcase()   # 大小写互换
Out[120]: ‘tEST‘

In [121]: ‘\t‘.expandtabs(4)   # 将table制表符转换为空格
Out[121]: ‘    ‘

In [123]: s.format()   # 单独讲,变化非常多,非常常用。
Out[123]: ‘test‘
upper:全部转换为大写
lower:全部转换为小写
title:标题,首字母大写
capitalize:首字母大写,常用于正文
center:标题居中
zfill:标题居右, 用0填充
casefold:忽略大小写
swapcase:大小写互换
expandtabs:将table制表符转换为空格

修改

In [124]: s = ‘i love python‘

In [125]: s.replace(‘love‘, ‘give up‘)  # 返回新字符串,使用new替换old
Out[125]: ‘i give up python‘

In [127]: s = ‘i very very love python‘

In [128]: s.replace(‘very‘, ‘not‘)   # 有多少个替换多少个
Out[128]: ‘i not not love python‘

In [129]: s.replace(‘very‘, ‘not‘, 1)   # 可选的count参数,指定替换多少次
Out[129]: ‘i not very love python‘

replace只能从前往后替换

In [130]: s = ‘     hahahahahah haha    ‘

In [131]: s.strip()   # strip移除字符串前后的空格
Out[131]: ‘hahahahahah haha‘

In [132]: s = ‘\n \r \t hahaha \t \n \r‘

In [133]: s.strip()   # strip移除前后的空白字符
Out[133]: ‘hahaha‘
注意:空格和空白是不一样的概念

In [135]: s = ‘######haha #### haha #####‘

In [136]: s.strip(‘#‘)   # 指定移除字符 
Out[136]: ‘haha #### haha ‘

In [138]: s = ‘{{ haha }}‘  

In [139]: s.strip(‘{}‘)    # 指定移除多个字符
Out[139]: ‘ haha ‘

In [140]: s.strip(‘{} ‘)   # 多加个空白,移除空白字符
Out[140]: ‘haha‘

In [141]: s.lstrip(‘{} ‘)   # lstrip表现和strip一样,但是只处理左端
Out[141]: ‘haha }}‘

In [142]: s.rstrip(‘{} ‘)   # rstrip表现和strip一样,但是只处理右端
Out[142]: ‘{{ haha‘

In [143]: s = ‘test‘   

In [144]: s.ljust(10)    # 原串在左边
Out[144]: ‘test      ‘

In [145]: s.rjust(10)    # 原串在右边
Out[145]: ‘      test‘

In [146]: s.center(10)   # 原串在中间
Out[146]: ‘   test   ‘

In [147]: s.center(10, ‘#‘)   # 原串在中间,使用指定字符填充
Out[147]: ‘###test###‘

In [148]: s.ljust(10, ‘#‘)   # 原串在右边,使用指定字符填充
Out[148]: ‘test######‘

In [149]: s.center(10, ‘##‘)   # 填充字符串长度只能为1
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-149-6e82a3cbddcf> in <module>()
----> 1 s.center(10, ‘##‘)

TypeError: The fill character must be exactly one character long

In [150]: s.center(3)   # 如果宽度小于等于原串长度,不做任何操作
Out[150]: ‘test‘

In [152]: s.center(11, ‘#‘)   # 优先填充左边
Out[152]: ‘####test###‘
replace:替换, 全替换
strip:移除字符串前后的空格
lstrip:移除左端的字符
rstrip:移除右端的字符
center:居中, 可以使用随意字符串填
ljust:原串在左边
rjust:原串在右边

查找

In [155]: s = ‘i love python‘

In [156]: s.find(‘love‘)   # 从左往右查找,找到第一个字串love,返回字串首字母的索引
Out[156]: 2 

In [157]: s.find(‘love1‘)   # 当字串不存在时,返回-1
Out[157]: -1

In [158]: s = ‘i very very love python‘

In [159]: s.find(‘very‘)    # 当有多个值的时候,返回第一个
Out[159]: 2

In [160]: s.find(‘very‘, 3)   # start参数指定从哪里开始查找
Out[160]: 7

In [161]: s.find(‘very‘, 3, 5)   # end参数指定到哪里结束查找, end不包含
Out[161]: -1

In [162]: s.rfind(‘very‘)   # rfind是find从右往左的版本
Out[162]: 7

In [163]: s.rfind(‘very‘, 3)  # start和stop意义一样,事实上, 是先根据start和stop截取字符串,再查找
Out[163]: 7

In [164]: s.rfind(‘very‘, 8)
Out[164]: -1

In [165]: s.index(‘very‘)   # 几乎与find一模一样
Out[165]: 2

In [166]: s.index(‘very‘, 3)
Out[166]: 7

In [167]: s.index(‘very‘, 8)    # 不存在的抛出ValueError, find查找不存在的时候返回-1, index和find惟一区别。
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-167-c5dce18db14c> in <module>()
----> 1 s.index(‘very‘, 8)

ValueError: substring not found

In [3]: s.rindex(‘very‘)    # rindex是index从右往左的版本
Out[3]: 7

In [5]: s.count(‘very‘)   # 存在
Out[5]: 2

In [6]: s.count(‘haha‘)   # 不存在返回0
Out[6]: 0

In [7]: s.count(‘very‘, 3)   # 从第三个开始统计
Out[7]: 1
s.find:查找, 从左至右
s.rfind:查找, 从右至左
s.index:根据值找出位置
s.rindex:是index的从右至左的版本
s.count:统计关键词出现的次数

判断

In [8]: s.startswith(‘i very‘)    # 判断字符串是否以某个前缀开始,返回结果是bool
Out[8]: True

In [9]: s.startswith(‘abc‘)
Out[9]: False

In [10]: s.startswith(‘very‘, 2)   # start参数表示的是从索引start处开始比较
Out[10]: True

In [11]: s.startswith(‘very‘, 2, 5)  # end参数表示从索引end处停止比较
Out[11]: False

In [12]: s.endswith(‘python‘)   # 判断字符串是否以某个后缀结尾, 返回bool
Out[12]: True

In [3]: s.endswith(‘python‘, 17)   # start参数表示从索引start处开始比较
Out[3]: True

In [5]: s.endswith(‘python‘, 0, 22)   # end参数表示到索引end为止, 不包含end
Out[5]: False

In [6]: ‘a‘.isalnum()   # 检测字符串是否由字母和数字组成;
Out[6]: True

In [81]: s.isalnum()   # 因为s含有空格, 所以返回False
Out[81]: False

In [82]: s.isidentifier()   # 字符是否是合法标示符
Out[82]: False

In [84]: ‘abc_123‘.isidentifier()
Out[84]: True

合法标示符:
    字母或者下划线开头
    仅包含字母数字和下划线

In [9]: ‘a‘.isidentifier()
Out[9]: True

string模块是补充自带字符方法不够的模块;
s.startswith:判断字符串是否以某个前缀开始,返回结果是bool
s.endswith:end参数表示从索引end处停止比较
s.isalnum:检测字符串是否由字母或数字,多个空格都不行
s.isdecimal:检查字符串是否只包含十进制字符,这种方法只存在于unicode对象。
s.islower:检测字符串是否由小写字母组成
s.isspace:检测字符串是否只由空格组成。
s.isalpha:检测字符串是否只由字母组成。
s.isdigit:检测字符串是否只由数字组成。
s.isnumeric:检测字符串是否只由数字组成,这种方法是只针对unicode对象。
s.istitle:检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写。
s.isascii:判断字符串是否只包含ASCII字符
s.isidentifier:用于判断字符串是否是有效的Python标识符,可用来判断变量名是否合法。
s.isprintable:如果所有字符都是可打印的,则isprintable()方法返回True,否则返回False。不可打印的字符可以是回车和换行符。
s.isupper:检测字符串中所有的字母是否都为大写

05-03字符串与其常用操作

标签:检查   path   索引   password   rom   移动   lin   相等   containe   

原文地址:https://www.cnblogs.com/cishi/p/12961600.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!