3.1python字符串类型
字符串是一个有序的,不可修改的,元素以引号包围的序列。
python字符串的定义,双引号或者单引号中的数据,就是字符串
a = 100
print(a,type(a)) #100 <class 'int'>
b = 'hello' #hello <class 'str'>
print(b,type(b))
字符串之间存储的另一种方式:字符串和字符串之间可以相加
a = 'lao'
b = 'zhang'
c = 'kang'
d = a + b
print(d) #laozhang
a = 100
b = 200
s = a + b
print(s,type(s)) #300 <class 'int'>
3.2 python 字符串的索引
比如 'while' 字符串,可以按照下图理解,索引从零开始
a = 'while'
b = a[2]
print(b) #i
3.3 python 字符串的切片
切片的语法:[起始:结束:步长]
字符串[start:end:step]和三个参数都有默认值
start:默认值为零
end:默认值为字符串结尾元素
step:默认值为1
字符串[start:end:step],对应索引元素的范围,该范围包含起始端,不包含结束端,默认的截取方向是从左向右的
s = 'hello'
print(s[0:2]) #he
print(2:2:2) #l
也可以反取,字符串[负数],从右向左取
s = 'hello'
print(s[-1:-3:-1]) # ol
注意 反取切片必须指定步长方向
3.4 字符串的方法
3.4.1 字符串的查找
count 计数功能,返回自定字符在字符串当中的个数
find 查找 返回从左向右第一个指定的字符,找不到返回-1
rfind 查找 返回从右向左第一个指定的字符,找不到返回-1
index 查找 返回从左往右第一个指定的字符,找不到报错
rindex 查找 返回从右向左第一个指定的字符,找不到报错
s = 'hello world python'
print(s.count('o')) #3
print(s.find('o')) #4
print(s.rfind('o')) #16
print(s.index('h')) #0
print(s.rindex('h')) #15
注意 index和find的区别是 find找不到返回-1 index找不到报错
3.4.2 字符串的拆分
字符串的分割 pratition 把字符串以指定的元素分成三部分 返回一个元组
splitlines 按照行分割,返回一个包含各行作为元素的列表,按照换行符分割
split 按照指定的内容进行分割
s = 'hello world python'
print(s.partition('world')) #('hello ', 'world', ' python')
print(s.split('o')) #['hell', ' w', 'rld pyth', 'n']
mystr="hello\nworld\npython\nand\nphp"
print(mystr.splitlines()) #['hello', 'world', 'python', 'and',
'php']
print("a\xa0a") # a a
3.4.3 字符串的替换
字符串的替换 replace 从左到右替换指定的元素,可以指定替换的个数,默认是全部替换
translate 按照对应关系来替换内容 from string import maketrans : 从字符串导入maketrans
s = 'hello world '
print(s.replace('o','y')) #helly wyrld
print(s.replace('o','y',1)) #helly world
3.4.4 字符串的修饰
center 让字符串在指定的长度中居中,如果不能居中左短右长,可以指定填充的内容,默认以空格填充
ljust 让字符串在指定的长度左对齐,可以指定填充的内容,默认以空格代替
rjust 让字符串在指定的长度右对齐,可以指定填充的内容,默认以空格代替
zfill 将字符串填充到指定的长度,不足的地方用"0"从左开始补充
strip 默认去除两边的空格,去除的内容可以指定
rstrip 默认去除右边的空格,去除的内容可以指定
lstrip 默认去除左边的空格,去除的内容可以指定
format 按照顺序,将后面的参数传递给前面的大括号
s = 'hello'
print(s.center(10)) # hello
print(s.center(10,'*')) #**hello***
print(s.ljust(10,'$')) #hello$$$$$
print(s.rjust(10,'$')) #$$$$$hello
print(s.zfill(10)) #00000hello
s1 = ' hello python '
s2 = '@@@hello python@@@'
print(s1.strip()) #hello python
print(s2.strip(' ')) #@@@hello python@@@
print(s2.lstrip('@')) #hello python@@@
print(s2.rstrip('@')) #@@@hello python
字符串的格式化
format{} 用法
相对基本格式化输出采用‘%’的方法,format()的功能更强大,该函数把字符串当成一个模板,通过传入的参数进行格式化,并且使用大括号“{}”,作为特殊字符代替“%”
1,使用位置参数
msg = '大家好我叫{},今年{}岁了'
print(msg.format('阿衰',10))
msg = '大家好我叫{0}!我叫{1}!我叫{0}!,今年{0}岁了!今年{1}岁了!今年{1}岁了!'
print(msg.format('阿衰',10))
结果:
大家好我叫阿衰,今年10岁了
大家好我叫阿衰!我叫10!我叫阿衰!,今年阿衰岁了!今年10岁了!今年10岁了!
2使用关键字参数
msg = '大家好我叫{name}!我叫{age}!我叫{name}!,今年{age}岁了!今年{age}
岁了!今年{name}岁了!'
print(msg.format(name = 'zs',age = 21))
结果:
大家好我叫zs!我叫21!我叫zs!,今年21岁了!今年21岁了!今年zs岁了!
3.填充与格式化
(1):[填充字符][对齐方式 <^>][宽度]
< 表示 向左对齐, ^ 表示居中对齐, >表示向右对齐
print('我叫{:*^5},今年{:<4}岁了'.format('zss',50))
我叫*zss*,今年50 岁了
4,精度与进制
print('长方形的宽是{:2f}cm,高是{:.3f},求长方形的面积'.format(3/2,5/3))
print('二进制{:b}'.format(10))
print('八进制{:o}'.format(10))
print('十六进制{:x}'.format(10))
结果:
长方形的宽是1.500000cm,高是1.667,求长方形的面积
二进制1010
八进制12
十六进制a
百分号格式
格式 | 描述 |
%% | 百分号标记 |
%c | 字符及其ASCII码 |
%s | 字符串 |
%d | 有符号整数(十进制) |
%u | 无符号整数(十进制)已过时 |
%o | 整数(八进制) |
%x | 整数(十六进制) |
%X | 整数(十六进制大写字符) |
%e | 浮点数字(科学计数法) |
%E | 浮点数字(科学计数法,用E代替e) |
%f | 浮点数字(用小数点符号) |
%g | 浮点数字(根据值的大小采用%e或%f) |
%G | 浮点数字(类似于%g) |
%p | 指针(用十六进制打印值的内存地址) |
%n | 存储输出字符的数量放进参数列表的下一个变量中 |