罪恶都市水上飞机,安琪 流泪的眼,evm2.1
用于移除字符串左右两边、左边、右边指定的字符(默认为空白符,例如:/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'
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'
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'
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))
>>> 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
统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。
str.count(sub, start= 0, end=len(string))
>>> 'hello world'.count('o') 2 >>> 'hello world'.count('o', 5 , len('hello world')) 1
把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。
str.replace(old, new[, 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
描述:
split:指定分隔符对字符串进行切片,如果参数 num 有指定值,则仅分隔 num 个子字符串。
splitlines:按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 false,不包含换行符,如果为 true,则保留换行符。
partition:根据指定的分隔符将字符串进行分割。如果字符串包含指定的分隔符,则返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。
rpartition:类似于partition()函数,只不过是从右边开始。
str.split(str="", num=string.count(str))
str.splitlines([keepends])
str.partition(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')
ljust:返回一个指定的宽度 width 居左的字符串,fillchar 为填充的字符,默认为空格。如果指定的长度小于原字符串的长度则返回原字符串。
center:返回一个指定的宽度 width 居中的字符串,fillchar 为填充的字符,默认为空格。如果指定的长度小于原字符串的长度则返回原字符串。
rjust:返回一个指定的宽度 width 居右的字符串,fillchar 为填充的字符,默认为空格。如果指定的长度小于原字符串的长度则返回原字符串。
str.ljust(width[, fillchar]) str.center(width[, fillchar]) str.rjust(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]'
将序列中的元素以指定的字符连接生成一个新的字符串。
str.join(sequence)
>>> '-'.join(('a', 'b', 'c')) 'a-b-c'
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
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Python 实现将numpy中的nan和inf,nan替换成对应的均值
python爬虫把url链接编码成gbk2312格式过程解析
网友评论