python——字符串 & 正则表达

raw字符串(原始字符串)

所见即所得,例如

print('\n')
print(r'\n')
\n
len('\n')
1
len(r'\n')
2

Unicode 字符串

  • ASCII码:每个字符都是以7位二进制数的方式存储在计算机内,ASCI字符只能表示95个可打印字符。
  • Unicode:通过使用一个或多个字节来表示一个字符的方式突破了ASCII码的限制。

示例:

'\nabc'
'\nabc'
u'\nabc'
'\nabc'
u'刘备'
'刘备'
U'卓越'
'卓越'

Python转义字符

在需要在字符中使用特殊字符时,python用反斜杠(\)转义字符。如下表:

转义字符描述
\(在行尾时)续行符
\\反斜杠符号
\'单引号
\"双引号
\a响铃
\b退格(Backspace)
\e转义
\000
\n换行
\v纵向制表符
\t横向制表符
\r回车
\f换页
\oyy八进制数,yy代表的字符,例如:\o12代表换行
\xyy十六进制数,yy代表的字符,例如:\x0a代表换行

格式化操作

python字符串格式化符号:

符   号描述
%c格式化字符及其ASCII码
%s格式化字符串
%u格式化无符号整型
%o格式化无符号八进制数
%x格式化无符号十六进制数
%X格式化无符号十六进制数(大写)
%f格式化浮点数字,可指定小数点后的精度
%e%E用科学计数法格式化浮点数
%g%f和%e的简写
%G%f%E 的简写
%p用十六进制数格式化变量的地址
%d格式化整数

格式化操作符辅助指令:

符号功能
*定义宽度或者小数点精度
-用做左对齐
+在正数前面显示加号(+ )
<sp>在正数前面显示空格
#在八进制数前面显示零('0'),在十六进制前面显示'0x'或者'0X'(取决于用的是'x'还是'X')
0显示的数字前面填充'0'而不是默认的空格
%'%%'输出一个单一的'%'
(var)映射变量(字典参数)
m.n.m 是显示的最小总宽度,n 是小数点后的位数(如果可用的话)

形式:
format%values

values的输入形式:

  • 元组形式
  • 字典形式(键作为format出现,值作为values存在)
print('hello %s, %s enough!'%('world','happy'))
hello world, happy enough!
print('int:%d,str:%s,str:%s'%(1.0,['in list','i am list'],'i am str'))
int:1,str:['in list', 'i am list'],str:i am str
'%x'%100
'64'
'%X'%110
'6E'
'we are at %d%%'%100
'we are at 100%'
'%s is %d years old'%('Li',20)
'Li is 20 years old'
for i in range(1000,10000):
    a=int(i/1000)
    b=int(i/100)%10
    c=(int(i/10))%10
    d=i%10
    if a**4+b**4+c**4+d**4==i:
        print('%d=%d^4+%d^4+%d^4+%d^4'%(i,a,b,c,d))
1634=1^4+6^4+3^4+4^4
8208=8^4+2^4+0^4+8^4
9474=9^4+4^4+7^4+4^4

m.n 宽度与精度

'%.3f'%123.12345
'123.123'
'%.5s'%'hello world'
'hello'
'%+d'%4
'+4'
'%+d'%-4
'-4'
from math import pi
'%-10.2f'%pi
'3.14      '
'%10.4f'%pi
'    3.1416'
'My name is %(name)s,age is %(age)d,gender is %(gender)s'%{'name':'LiMing','age':28,'gender':'male'}
'My name is LiMing,age is 28,gender is male'

字符串模板

字符串对象Template对象存在与string模块中:

  • 使用美元符号$定义代替换的参数
  • 使用substitute()方法(缺少参数时会报错,KeyError异常) & safe_substitute()方法(缺少key时,直接显示参数字符串)进行参数替换

示例:

from string import Template
s=Template('There are ${how_many} nodes in the ${tree}')
print(s.substitute(how_many=32,tree='splay_tree'))
There are 32 nodes in the splay_tree
print(s.substitute(how_many=32))
---------------------------------------------------------------------------

KeyError                                  Traceback (most recent call last)

<ipython-input-4-6c5e84463638> in <module>()
----> 1 print(s.substitute(how_many=32))


D:\ProgramData\Anaconda3\lib\string.py in substitute(*args, **kws)
    124             raise ValueError('Unrecognized named group in pattern',
    125                              self.pattern)
--> 126         return self.pattern.sub(convert, self.template)
    127 
    128     def safe_substitute(*args, **kws):


D:\ProgramData\Anaconda3\lib\string.py in convert(mo)
    117             named = mo.group('named') or mo.group('braced')
    118             if named is not None:
--> 119                 return str(mapping[named])
    120             if mo.group('escaped') is not None:
    121                 return self.delimiter


KeyError: 'tree'
from string import Template
s=Template('There are ${how_many} nodes in the ${tree}')
print(s.safe_substitute(how_many=32))
There are 32 nodes in the ${tree}

正则表示

import re
# 判断字符串是否由‘hello’开始
pattern=re.compile('hello') #得到匹配字符串‘hello’的正则表示对象
res=pattern.match('hello world') #使用match方法进行匹配
if res :
    print(res.group()) # 使用group方法得到匹配内容
else:
    print('not match')
hello
# 查找字符串是否包含'hello'
pattern=re.compile('hello') #得到匹配字符串‘hello’的正则表示对象
res=pattern.search('world hello') #使用search方法进行匹配
if res :
    print(res.group()) # 使用group方法得到匹配内容
else:
    print('not match')
hello
# 查找字符串中包含'hello'的所有部分
pattern=re.compile('hello') #得到匹配字符串‘hello’的正则表示对象
res=pattern.findall('world hello world hello') #使用findall方法得到所有匹配内容
print(res)
['hello', 'hello']

.

import re
pattern=re.compile(r'ab.xy')
res=pattern.match(r'ab0xy')
if res:
    print(res.group())
ab0xy

|

import re
pattern=re.compile(r'abc|xyz')
res=pattern.match(r'abc')
if res:
    print(res.group())
abc
res=pattern.match(r'xyz')
if res:
    print(res.group())
xyz

[ ]

import re 
pattern=re.compile(r'[ab][xy][01]')
res=pattern.match(r'ax0')
if res:
    print(res.group())
ax0
import re
html = 'Hello <a href="https://www.biaodainfu.com">biaodianfu</a>'
m = re.findall('<a.*>.*<\/a>', html)
if m:
    print(m)
['<a href="https://www.biaodainfu.com">biaodianfu</a>']
import re
html = 'Hello <a href="https://www.biaodainfu.com">biaodianfu</a> | Hello <a href="https://www.google.com">Google</a>'
m = re.findall('<a.*>.*<\/a>', html)
if m:
    print(m)
['<a href="https://www.biaodainfu.com">biaodianfu</a> | Hello <a href="https://www.google.com">Google</a>']

Python 的字符串内建函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值