1、Str 的常用操作
1.1字符串的拼接
>>> a = 'hello'
>>> b = 'python'
>>> a + '-' + b
'hello-python'
>>> a + b
'hellopython'
但是字符串和非字符串不能拼接
要想拼接得使用字符串的类型转换 str()
>>> a + str(123)
'hello123'
1.2 长度计算 len()
>>> len('我要好好学习python')
12
1.3 截取
字符串是一种特殊的序列,对于列表和元组对它同样适用
>>> a = 'hello'
>>> b = 'python'
>>> a[1]
'e'
>>> 'aaabbbccc'[8]
'c'
>>> b[-2]
'o'
>>> a[1:2]
'e'
>>> a[1:3]
'el'
>>> a[:4]
'hell'
1.4 分隔 split()
用空格和逗号
>>> '1 2 3 4 5'.split()
['1', '2', '3', '4', '5']
>>> '1 2 3 4 5'.split()
['1', '2', '3', '4', '5']
>>> '1,2,3,4,5'.split(',')
['1', '2', '3', '4', '5']
1.5 连接
列表中的每一个元素都用#这个字符
>>> '#'.join(['1','2','3','4','5'])
'1#2#3#4#5'
>>> '#'.join('1 2 3 4 5'.split())
'1#2#3#4#5'
1.6 检索
count() 查找子字符串在字符串中出现的次数
>>> a = 'hello'
>>> a.count('e')
1
>>> a.count('l')
2
>>> a.count('llo')
1
find() 查找子字符串在字符串中出现的位置,若没有,返回的是-1
>>> a = 'hello'
1
>>> a.find('e')
1
>>> a.find('ll')
2
>>> a.find('a')
-1
2个参数时,从自定义起始位置查找包含e的字符串
>>> a = 'hello'
>>> a.find('e',2)
-1
>>> a.find('e',1)
1
index() 也是查找子字符串在字符串中出现的位置
>>> a.index('e')
1
>>> a.index('ll')
2
find()和index()的区别就是:查找不存在的字符,index会报错,find会返回-1,可根据方法选择
rindex 从后往前检索
index 是从前往后检索
>>> a.rindex('l')
3
>>> a.index('l')
2
a.startswith 判断一个字符串是以什么开头,返回布尔是类型
>>> a = 'hello'
>>> a.startswith('hel')
True
>>> a.startswith('abc')
False
1.7 大小写转换
upper()
lower()
>>> a = 'hello'
>>> a.upper()
'HELLO'
>>> b = 'HELLO'
>>> b.lower()
'hello'
1.8 除空格和特殊字符
去除空白符
strip() 去除字符串两端空白符
lstrip() 去除字符串左端空白符
rstrip() 去除字符串右端空白符
>>> ' hello '.strip()
'hello'
>>> ' hello '.lstrip()
'hello '
>>> ' hello '.rstrip()
' hello'
去除特殊字符
原理与上述相同,括号中需调入特殊字符
' hello'
>>> '###hello#'.strip('#')
'hello'
>>> '###hello#'.lstrip('#')
'hello#'
>>> '###hello#'.rstrip('#')
'###hello'
2、字符串的格式化输出
2.1 %
占位符
>>> '我是一个新的字符串,这有一个数字 %d,这有一个字符串 %s ,这有一个浮点数 %f' % (10,'abc',1.666)
'我是一个新的字符串,这有一个数字 10,这有一个字符串 abc ,这有一个浮点数 1.666000'
>>> '%d,%s,%f' %(3,'abcd',1.333)
'3,abcd,1.333000'
%08d 表示一共8位,左边7位用0填充
>>> '字符串 %d' % 1
'字符串 1'
>>> '字符串 %8d' % 1
'字符串 1'
>>> '字符串 %08d' % 1
'字符串 00000001'
浮点数小数点后保存两位 %.2f
>>> '浮点数 %.2f' % 1.6666666
'浮点数 1.67'
用0填充左边,算上小数点一共5位
>>> '浮点数 %05.2f' % 1.6666666
'浮点数 01.67'
若占位符只有一个的话,%后面的参数直接用要使用单一的即可;若有多个占位符,后面用元组
2.2 format
放一个占位符即可,放的是可变参数
>>> '我是一个新的字符串,这有一个数字 {},这有一个字符串 {} ,这有一个浮点数 {}'.format(10,'abc',1.333)
'我是一个新的字符串,这有一个数字 10,这有一个字符串 abc ,这有一个浮点数 1.333'
比如金融上表示钱的方式
f表示浮点类型
:表示参数的开始
, 表示对数字以千为单位分隔
.2表示表示小数点后保留2位
>>> '金额表示: ¥{:,.2f}元'.format(10000)
'金额表示: ¥10,000.00元'
十六进制
>>> '{0:d}的十六进制{0:#x}'.format(100)
'100的十六进制0x64'
3、字符编码
编码 encode()
>>> a = '人生苦短,我用python'
>>> len(a)
13
>>> a.encode()
b'\xe4\xba\xba\xe7\x94\x9f\xe8\x8b\xa6\xe7\x9f\xad\xef\xbc\x8c\xe6\x88\x91\xe7\x94\xa8python'
>>> a.encode('UTF8')
b'\xe4\xba\xba\xe7\x94\x9f\xe8\x8b\xa6\xe7\x9f\xad\xef\xbc\x8c\xe6\x88\x91\xe7\x94\xa8python'
>>> a.encode('GBK')
b'\xc8\xcb\xc9\xfa\xbf\xe0\xb6\xcc\xa3\xac\xce\xd2\xd3\xc3python'
>>> len(a.encode())
27
>>> len(a.encode('GBK'))
20
解码 decode()
>>> a.encode().decode()
'人生苦短,我用python'
解码的时候GBK 和 utf-8 要是不对应会出现乱码,要是一个乱码可以尝试以下另外一个
>>> a.encode().decode()
'人生苦短,我用python'
>>> a.encode().decode('GBK')
'浜虹敓鑻︾煭锛屾垜鐢╬ython'
3、正则表达式
正则表达式,又称正规表示式、正规表示法、正规表达式、规则表达式、常规表示法(英语:Regular Expression,在代码中常简写为regex、regexp或RE),是计算机科学的一个概念。正则表达式使用单个字符串来描述、匹配一系列匹配某个句法规则的字符串。在很多文本编辑器里,正则表达式通常被用来检索、替换那些匹配某个模式的文本。
比如说,判断输入的手机号是否合法
先引入正则表达式的模块 import re
r代表模式,\d代表数字
(第一位确定1,第二位可能的情况写[]中,剩下的就是9位,然后来验证)
>>> import re
>>> f = '13000000000'
>>> g = '18604385672'
>>> re.match(r"^1[3456789]\d{9}$",f)
<re.Match object; span=(0, 11), match='13000000000'>
>>> re.match(r"^1[3456789]\d{9}$",g)
<re.Match object; span=(0, 11), match='18604385672'>