当前位置: 移动技术网 > IT编程>脚本编程>Python > Python 常见的字符串操作

Python 常见的字符串操作

2018年08月18日  | 移动技术网IT编程  | 我要评论

罪恶都市水上飞机,安琪 流泪的眼,evm2.1

1、strip、lstrip和rstrip

描述:

用于移除字符串左右两边、左边、右边指定的字符(默认为空白符,例如:/n, /r, /t, ' ')或字符序列。

语法:

str.strip([chars])
str.lstrip([chars])
str.rstrip([chars])

例如:

1)移除单个字符或空白:

>>> '  abc '.strip()
'abc'

>>> '  abc'.lstrip()
'abc'

>>> 'abc  '.rstrip()
'abc'

>>> 'abc'.strip('a')
'bc'

>>> 'abc'.lstrip('a')
'bc'

>>> 'abc'.rstrip('c')
'ab'
view code

2)移除一个字符串列表(是否会删除的前提是从字符串最开头和最结尾是不是包含要删除的字符,如果有就会继续处理,没有的话是不会删除中间的字符的):

>>> 'abc@163.com'.strip('cawm')
'bc@163.co'

>>> 'abc@163.com'.lstrip('cawm')
'bc@163.com'

>>> 'abc@163.com'.rstrip('cawm')
'abc@163.co'
view code

 2、大小写转换lower、upper、title、capitalize、swapcase

描述:

lower:将字符串中的大写字母转为小写字母。

upper:将字符串中的小写字母转为大写字母。

title:将所有单词首字母转为大写,其余字母均转为小写。

capitalize:将字符串的第一个字母转为大写,其他字母转为小写。

swapcase:将字符串做大小写字母转换(大写->小写,小写->大写)

语法:

str.lower()
str.upper()
str.title()
str.capitalize()
str.swapcase()

例如:

>>> 'abcde'.upper()
'abcde'

>>> 'abcde'.lower()
'abcde'

>>> 'this is a example'.title()
'this is a example'

>>> 'this is a example'.capitalize()
'this is a example'

>>> 'abcde'.swapcase()
'abcde'
view code

 3、find、index、rfind、rindex

描述:

find:检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-1。

index:检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,该方法与 python find()方法一样,只不过如果str不在 string中会报一个异常。

rfind:类似于find()函数,只不过是从字符串右边开始查找。

rindex:类似于index()函数,只不过是从字符串右边开始查找。

语法:

str.find(str, beg=0, end=len(string))
str.index(str, beg=0, end=len(string))
  • str -- 指定检索的字符串
  • beg -- 开始索引,默认为0。
  • end -- 结束索引,默认为字符串的长度。

例如:

>>> str1 = 'this is a example!'
... str2 = 'example'
... index = str1.find(str2, 0, len(str1))
... print(index)
10

>>> str1 = 'this is a example!'
... str2 = 'example'
... index = str1.index(str2, 0, len(str1))
... print(index)
10
view code

 4、count

描述:

统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。

语法:

str.count(sub, start= 0, end=len(string))

例如: 

>>> 'hello world'.count('o')
2

>>> 'hello world'.count('o', 5 , len('hello world'))
1
view code

 5、replace

描述:

把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。

语法:

str.replace(old, new[, max])
  • old -- 将被替换的子字符串。
  • new -- 新字符串,用于替换old子字符串。
  • max -- 可选字符串, 替换不超过 max 次。

例如:

>>> str = 'this is a example'
... nstr = str.replace('is', 'was')
... print(nstr)
thwas was a example

>>> str = 'this is a example'
... nstr = str.replace('a', 'some', 1)
... print(nstr)
this is some example
view code

 6、split、splitlines、partition、rpartition

描述:

split:指定分隔符对字符串进行切片,如果参数 num 有指定值,则仅分隔 num 个子字符串。

splitlines:按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 false,不包含换行符,如果为 true,则保留换行符。

partition:根据指定的分隔符将字符串进行分割。如果字符串包含指定的分隔符,则返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。

rpartition:类似于partition()函数,只不过是从右边开始。

语法:

str.split(str="", num=string.count(str))
  • str -- 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
  • num -- 分割次数。
str.splitlines([keepends])
  • keepends -- 在输出结果里是否保留换行符('\r', '\r\n', \n'),默认为 false,不包含换行符,如果为 true,则保留换行符。
str.partition(str)
  • str-- 指定的分隔符。
str.rpartition(str)

例如:

 

>>> 'this \nis a \nexample'.split()
['this', 'is', 'a', 'example']

>>> 'this \nis a \nexample'.split(' ', 1)
['this', '\nis a \nexample'

>>> 'this \nis a \nexample'.splitlines()
['this ', 'is a ', 'example']

>>> 'this \nis a \nexample'.splitlines(true)
['this \n', 'is a \n', 'example']

>>> 'www.example.com'.partition('.')
('www', '.', 'example.com')

>>> 'www.example.com'.rpartition('.')
('www.example', '.', 'com')
view code

7、填充ljust、center、rjust

描述:

ljust:返回一个指定的宽度 width 居左的字符串,fillchar 为填充的字符,默认为空格。如果指定的长度小于原字符串的长度则返回原字符串。

center:返回一个指定的宽度 width 居中的字符串,fillchar 为填充的字符,默认为空格。如果指定的长度小于原字符串的长度则返回原字符串。

rjust:返回一个指定的宽度 width 居右的字符串,fillchar 为填充的字符,默认为空格。如果指定的长度小于原字符串的长度则返回原字符串。

语法:

str.ljust(width[, fillchar])
str.center(width[, fillchar])
str.rjust(width[, fillchar])
  • width -- 字符串的总宽度。
  • fillchar -- 填充字符。

例如:

>>> '[www.example.com]'.ljust(30, '*')
'[www.example.com]*************'

>>> '[www.example.com]'.rjust(30, '*')
'*************[www.example.com]'

>>> '[www.example.com]'.center(30, '*')
'******[www.example.com]*******'

>>> '[www.example.com]'.center(4, '*')
'[www.example.com]'
view code

 8、join

描述:

将序列中的元素以指定的字符连接生成一个新的字符串。

语法:

str.join(sequence)
  • sequence -- 要连接的元素序列。

例如:

>>> '-'.join(('a', 'b', 'c'))
'a-b-c'
view code

 9、isalpha、isdigit、isalnum、isspace

描述:

isalpha:检测字符串是否只由字母组成。

isdigit:检测字符串是否只由数字组成。

isalnum:检测字符串是否由字母和数字组成。

isspace:检测字符串是否只由空格组成。

语法:

str.isalpha()
str.isdigit()
str.isalnum()
str.isspace()

例如:

>>> 'abc'.isalpha()
true

>>> '123'.isdigit()
true

>>> 'abc123'.isalnum()
true

>>> '   '.isspace()
true
view code

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网