好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Python基础之字符串介绍

Python字符串介绍: python3字符串编码格式:在内存中 是使用Unicode编码的,也就是能够支持多语言

print('输出的是字符串str') 在内存中的字符串类型数据存储到磁盘就要转换为bytes 类型的数据,x=b'ABC'是byte类型数据,y=‘ABC'是字符串类型数据,byte类型数据一个字符只占一个字节

字符串相关函数: ord():将单个字符串转换为整数,chr():将整数转换为单个字符

ord('A') 65 ord('姚') 23002 chr(65) 'A' chr(23002) '姚'

如果知道字符串的整数编码,可以十六进制表示字符串:

'\u59da\u6f47\u950b' '姚潇锋'

encode():将unicode编码的字符串按照特定的编码方式编码成bytes(内存中的数据存储到硬盘的过程)

'ABC'.encode('ASCII') b'ABC' '姚潇锋'.encode('GB2312') b'\xd2\xa6\xe4\xec\xb7\xe6' '姚潇锋'.encode('utf-8')) b'\xe5\xa7\x9a\xe6\xbd\x87\xe9\x94\x8b'

decode():将bytes数据转换为str(从网络或者硬盘读取字节流的过程)

b'\xe5\xa7\x9a\xe6\xbd\x87\xe9\x94\x8b'.decode('utf-8') '姚潇锋' b'\xd2\xa6\xe4\xec\xb7\xe6'.decode('GB2312') '姚潇锋' b'ABC'.decode('utf-8') 'ABC'

如果bytes中有无法解码的字节,就会报错,可以用errors='ignore’来忽略

b'\xe5\xa7\x9a\xe6\xbd\x87\xe9\xff\x8b'.decode('utf-8') Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 6: invalid continuation byte

b'\xe5\xa7\x9a\xe6\xbd\x87\xe9\xff\x8b'.decode('utf-8',errors='ignore') '姚潇'

len():计算字符串包含多少个字符,计算bytes包含多少个字节

len('ABC') 3 len('姚潇锋') 3 len('姚潇锋'.encode('utf-8')) 9 len('姚潇锋'.encode('GB2312')) 6 len(b'\xe5\xa7\x9a\xe6\xbd\x87\xe9\x94\x8b') 9 从上面可以看出一个中文字符使用GB2312编码后是两个字节,通过utf-8编码后是三个字节 一个英文字符使用何种方式编码都是只有一个字节

操作字符串注意事项: 在操作字符串时,经常会遇到str和bytes的转换,因此容易出现乱码问题,未来避免这个,需要修改编辑器编码方式和代码文件头部增加的内容: 编辑器编码方式要改为“使用utf-8 without bom 编码” 在代码文件开头添加 #!/usr/bin/env python3 //告诉linex或mac操作系统这是一个可执行的程序,Windows会忽略这一行

‘- - coding: utf-8 - -’ //告诉python解释器,读取代码时使用utf-8方式

字符串格式化: 有两种方式来对字符串进行格式化,一种是%s,另一种是format()

第一种:和C语言一样使用%来格式化

占位符 替换内容 %s 字符串 %d 整数 %f 浮点数 %x 十六进制整数 case:

'hello,%s,i am %s' %('姚潇锋','Amy') 'hello,姚潇锋,i am Amy'

其中,整数和浮点数可以指定位数和是否带0 case: 程序内容如下:

- - coding: utf-8 - -

print('中文测试') print('%d-%4d' %(5,7)) print('%3d-%4d' %(5,7)) print('%3d-%04d' %(5,7)) print('%3d-%004d' %(5,7)) print('%3d-%0004d' %(5,7))

print('%0.2f' %3.1415926) 输出为: (xiao) C:\Users\cdata\Desktop>python test1.py 中文测试 5- 7 5- 7 5-0007 5-0007 5-0007 3.14 备注: 在不确定使用何种数据类型时,可以使用%s,他可以把后面任何类型的数据转换为字符串 当%当作普通字符时,使用%%作为来进行转义,这样%就只是一个字符的含义。 print('Age:%s,Name:%s' %(15,'xiao')) print('growth rate is %s %%' %7)

第二种:使用format()格式化 使用传入的参数一次替代前面的占位符{0},{1}..... 另外一个区别是参数前的%前面是空格,format()前使用的是'.' case: print('My name is {0},my weight is {1:.2f}kg'.format('xiao',70.234))

 练习代码:
# -*- coding: utf-8 -*- 

print('中文测试') print('%d-%4d' %(5,7)) print('%3d-%4d' %(5,7)) print('%3d-%04d' %(5,7)) print('%3d-%004d' %(5,7)) print('%3d-%0004d' %(5,7)) #补零时只需要输入一个0即可 print('%0.5f' %0.001415926) print('%.5f' %0.001415926) #小数会自动补零 print('Age:%s,Name:%s' %(15,'xiao'))

print('growth rate is %s %%' %7)

print('My name is {0},my weight is {1:.2f}kg'.format('xiao',70.234))

#练习: #小明的成绩从去年的72分提升到了今年的85分,请计算小明成绩提升的百分点,并用字符串格式化显示出'xx.x%',只保留小数点后1位:

s1 = 72 s2 = 85 r = (s2-s1)/s1 print('小明成绩提升的了%0.1f%%' %r)

查看更多关于Python基础之字符串介绍的详细内容...

  阅读:22次