1.replace() :方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。
>>> s="hello world"
>>> s.replace("world","Lily")
'hello Lily'
>>> s.replace("l","*")
'he**o wor*d'
2.expandtabs():把字符串中的 tab 符号('\t')转为空格,tab 符号('\t')默认的空格数是 8。
>>> s="a\tb"
>>> s
'a\tb'
>>> print(s)
a b
>>> print(s.expandtabs())
a b
>>> print(s.expandtabs(1))
a b
>>> print(s.expandtabs(4))
a b
>>> print(s.expandtabs(8))
a b
3.split(): 通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则分隔 num+1 个子字符串
-
str.split(str="", num=string.count(str))
-
str -- 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
-
num -- 分割次数。默认为 -1, 即分隔所有。
>>> s="hello-world-hello-boy"
>>> s.split("-")
['hello', 'world', 'hello', 'boy']
>>> s.split("-",2)
['hello', 'world', 'hello-boy']
splitlines():按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。
-
S.splitlines([keepends])
>>> "a\nb c\rd\r\nd".split()
['a', 'b', 'c', 'd', 'd']
>>> "a\nb c\rd\r\nd".splitlines()
['a', 'b c', 'd', 'd']
>>> "a\nb c\rd\r\nd".splitlines(1)
['a\n', 'b c\r', 'd\r\n', 'd']
rsplit():从右侧进行分割
>>> s="hello-world-hello-boy"
>>> s.rsplit("-",2)
['hello-world', 'hello', 'boy']
4.join():将序列中的元素以指定的字符连接生成一个新的字符串。
序列类型包括字符串、列表、元组、集合和字典,这些序列支持以下几种通用的操作,但比较特殊的是,集合和字典不支持索引、切片、相加和相乘操作。
>>> s
'hello-world-hello-boy'
>>> b=s.split("-")
>>> b
['hello', 'world', 'hello', 'boy']
>>> "||".join(b)
'hello||world||hello||boy'
5.startswith() :用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。如果参数 beg 和 end 指定值,则在指定范围内检查。
-
endswith():检查字符串时候一指定字符串结尾
>>> s
'hello-world-hello-boy'
>>> s.startswith("hello")
True
>>> s.startswith("boy")
False
>>> s.startswith("hello",2)
False
>>> s.startswith("hello",12)
True
>>> s.rfind("hello")
12
>>> s.endswith("boy")
True
>>> s.endswith("hello",0,17)
True
6.isalpha():检测字符串是否只由字母组成。
-
isdigit():检测字符串是否只由数字组成。
-
isalnum():检查字符串是否只由字母和数字组成
-
isspace():检查字符串是否只由空格组成,字符串至少有一个字符
-
islower():检查字符串是否全为小写
-
isupper():检查字符串是否全为大写
-
istitle():检查字符串所有单词的首字母是否为大写
s='hello-world-hello-boy'
>>> s.isalpha()
False
>>> (" ".join(s.split("-"))).isalpha()
False
>>> ("".join(s.split("-"))).isalpha()
True
>>> "123".isdigit()
True
>>> "123abc".isdigit()
False
>>> "123abc".isalnum()
True
>>> "123".isalnum()
True
>>> " ".isspace()
True
>>> "".isspace()
False
>>> "\n\r\r\n".isspace()
True
>>> "abc".islower()
True
>>> "abc".isupper()
False
>>> "ABC".isupper()
True
>>> "A B C".istitle()
True
>>> "A a C".istitle()
False
>>> "a B C".istitle()
False
7.maketrans():法用于创建字符映射的转换表,对于接受两个参数的最简单的调用方式,第一个参数是字符串,表示需要转换的字符,第二个参数也是字符串表示转换的目标。
-
注:两个字符串的长度必须相同,为一一对应的关系
-
translate() 方法根据参数table给出的表(包含 256 个字符)转换字符串的字符, 要过滤掉的字符放到 del 参数中。
>>> map=str.maketrans("123","abc")
>>> "123e 231d".translate(map)
'abce bcad'
-
Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一
-
Base64编码可用于在HTTP环境下传递较长的标识信息。
>>> import base64
>>> encodestr=base64.b64encode(b"I love you")
>>> print(encodestr)
b'SSBsb3ZlIHlvdQ=='
>>> print(base64.b64decode(encodestr))
b'I love you'
判断类型
>>> isinstance("a",str)
True
>>> isinstance("a",int)
False
>>> isinstance(12.1,float)
True
>>> isinstance([12.1],list)
True
>>> isinstance((12.1),tuple)
False
>>> isinstance((12.1,),tuple)
True
>>> isinstance({1:2},dict)
True
>>> isinstance(1,(int,float))
True
count():用于统计某个元素出现的次数。
>>> "hello hello hello".count("l")
6
>>> "hello hello hello".count("hello")
3