string模块
import string
string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'string.digits
'0123456789'string.punctuation
'!"#$%&\'()*+,-./:;<=>?@[\]^_`{|}~'string.octdigits
'01234567'string.hexdigits
'0123456789abcdefABCDEF'
利用string.punctuation去除特殊字符
s = "I am a boy, you r a girl! yes!"
list(map(lambda x:x.strip(string.punctuation),s.split()))
['I', 'am', 'a', 'boy', 'you', 'r', 'a', 'girl', 'yes']
lower()
转换为小写
"ABC".lower()
'abc'"A".lower() =="a"
True
"a".lower() =="a"
True
upper()
转为大写
"abc".upper()
'ABC'
capitalize()
首字母大写
"you are".capitalize()
'You are'
title()
每个单词首字母大写
"you are".title()
'You Are'
swapcase()
大小写转换
"ABC xyz".swapcase()
'abc XYZ'"aba".swapcase()
'ABA'
string.capwords()
String 模块的大小写转换行数
string.capwords("abc ddd")
'Abc Ddd'
ljust(width[, fillchar])
左对齐,长度不足n,用指定字符填充,默认空格填充
"abc".ljust(10,"*")
'abc***'
"abc".ljust(10)
'abc '
rjust(width[, fillchar])
右对齐,长度不足n,用指定字符填充,默认空格填充
"abc".rjust(10)
' abc'
"abc".rjust(10,"*")
'***abc'
center(width[, fillchar])
居中对齐,长度不足n,用指定字符填充,默认空格填充
"abc".center(10)
' abc '
"abc".center(10,"#")
'###abc####'
zfill(width)
把S变成width长,并在右对齐,不足部分用0补足
"123".zfill(8)
'00000123'"abc".zfill(10)
'0000000abc'
find(substr[,star[,end]])
在指定范围内搜索字符串,找不到返回-1,找到返回索引位置,从左侧开始搜索
"123".find("2")
1"abc".find("aa")
-1
"abc".find("a",2)
-1
"abc".find("a",1,3)
-1
index(substr[,star[,end]])
在指定范围内搜索字符串,找不到报错,找到返回索引位置"abc".index("a")
0"abc".index("aa")
Traceback (most recent call last):
ValueError: substring not found
rfind(substr[,star[,end]])
在指定范围内搜索字符串,找不到返回-1,找到返回索引位置,从右侧开始搜索
"abcda".rfind("a")
4
"abcda".rfind("aa")
-1
rindex(substr[,star[,end]])
"abca".rindex("a")
3
replace(old, new[, count])
可指定替换次数
"abcda".replace("ab","x")
'xcda'
"abcda".replace("ab","x",3)
'xcda'
"abcda".replace("a","x",1)
'xbcda'
删除空格
"ab cd ef dd".replace(" ","")
'abcdefdd'
expandtabs
Tab空格替换成1个空格
"a c d d".expandtabs(1)
'a c d d'"a c d d".replace(" "," ")
'a c d d'
"a c d d".replace("\t"," ")
'a c d d'
split(split_str,times)
按指定字符分割字符串,可指定分割次数,没有指定默认全部分割
"a b c d".split()
['a', 'b', 'c', 'd']
"a\nb\tc\rd".split()
['a', 'b', 'c', 'd']"abcd".split("",1)
['a', 'bcd']
rsplit(split_str,times)
"a b c d".rsplit(" ",1)
['a b c', 'd']
"abcd".rsplit("",2)
['a*b', 'c', 'd']
s.splitlines([keepends])
把S按照行分割符分为一个list,keepends是一个bool值,如果为真每行后而
会保留行分割符
s = "1\n2\n"
s.splitlines()
['1', '2']
s.splitlines(1)#1表示输出换行符
['1\n', '2\n']s = "1\n2\n"
s.splitlines()
['1', '2']
s.splitlines(1)
['1\n', '2\n']
s.splitlines(True)
['1\n', '2\n']
s.splitlines(False)
['1', '2']
s.splitlines(0)
['1', '2']
join(iterable)
将可迭代对象拼接成字符串
拼接列表
"".join(["1","2","3"])
'123'
拼接元组
"".join(("1","2","3"))
'123'
拼接字典,只对key有效
"".join({"a":1,"b":2})
'ab'
拼接集合
"".join({"a","b"})
'ba'
startswith(prefix[, start[, end]])
"abc d".startswith("a")
True
endswith(prefix[, start[, end]])
"abc d".endswith("a")
False
isalpha()
"ab".isalpha()
True
isdigit()
"ab".isdigit()
False
isalnum()
"ab2".isalnum()
True
isspace()
"\n".isspace()
True
"\n\r\t".isspace()
True
习题6:判断一下这句话,有几个数字和几个空白,和几个字母其他字符有几个
"I am a 12 years old boy! hi,me!"
s = "I am a 12 years old boy! hi,me"
d_num =0
letter_num = 0
space_num = 0
other_num = 0
for v in s:
if v.isalpha():
letter_num += 1
elif v.isdigit():
d_num += 1
elif v.isspace():
space_num += 1
else:
other_num += 1
print(d_num)
print(letter_num)
print(space_num)
print(other_num)
islower()
"abc".islower()
True
isupper()
"abc".isupper()
False
istitle()
是否只有首字母都是大写
"AB DDD".istitle()
False
"Ab Dddd".istitle()
True
maketrans
map = str.maketrans('123', 'abc')
s = '54321123789'
print (s.translate(map))
t=bytes.maketrans(b'abc',b'ABC')
print (b'abc123'.translate(t,b"123"))
替换abc,并删除123
print (b'abc321'.translate(t,b"123"))
b'ABC'
判断字符集
import chardet
chardet.detect("的的的方式的离开尖峰时刻连接方式尖峰时刻积分".encode("gbk"))
{'encoding': 'GB2312', 'confidence': 0.99, 'language': 'Chinese'}
Base64编、解码
import base64
encodestr = base64.b64encode(b'I love you')
print(encodestr)
print(base64.b64decode(encodestr))
Isinstance()判断类型
isinstance("a",str)
True
isinstance(b"a",bytes)
True
isinstance(b"a",(str,bytes))
True
Python3 ord chr
ord("的")
30340
chr(33333)
'舵'
chr(33335)
'舷'
count(sub[, start[, end]])
"ab".count("a")
1
"ab".count("a",2)
0
[1,1,2,3].count(1)
2
(1,2,3,4).count(1)
1
自定义split
class MyStrMethod(object):
@classmethod
def split2(cls,arr,split_str=None,times=None):
if split_str == None:
split_str = " "
if times== None:
times = arr.count(split_str)
split_str_length = len(split_str)
print(split_str_length)
print(split_str)
result = []
index = 0
while index < len(arr) and times >0:
temp = ""
for i in range(index,len(arr)):
if arr[i:i+split_str_length] != split_str:
temp += arr[i]
index += 1
else:
index += split_str_length
break
result.append(temp)
times -= 1
if times < len(arr):
result.append(arr[index:])
return result
m = MyStrMethod()
print(m.split("a*b*c*d","*"))
print(m.split2("a*b*c*d","*"))
print(m.split2("a b c d"))
print(m.split2("a*b*c*d","*",1))
print(m.split2("a**b**c**d","**"))
print(m.split2("a**b**c**d","**",2))
转载于:https://blog.51cto.com/13496943/2308792