Python day2 part1:字符串编码

字符串编码方式主要有两种:ASCII码、Unicode码和UTF-8码

Unicode和UTF-8码支持汉字

UTF-8码是可变长的,UTF-8中英语字符通常是1个字节,而汉字通常为3个字节,比Unicode更加节约

  • 在计算机中

 内存、记事本里面使用Unicode编码,在文件或者传输途中使用UTF-8编码

  • 在网页中

 服务器里面是Unicode,而网页上提供给浏览
器的是UTF-8

1.字符串相关函数

a.      字符转化

         ord变汉字或英语字符为整数表示

   chr将整数表示换回字符

print(ord('A'))
print(ord('字'))
print(chr(65))
print(chr(25991))
结果:
65
23383
A
文
b.    字符串转化

 encode()可以将字符串变为指定形式的编码,decode()反之

 字符串与encode()和decode()之间使用.连接

 ()中间可以使用大写也可以使用小写,ascii与ASCII、utf-8与UTF-8都行

print('hello world'.encode('ascii'))
print('北京航空航天大学'.encode('utf-8'))
print('北京航空航天大学'.encode('UTF-8'))
print(b'hello world'.decode())
print(b'\xe5\x8c\x97\xe4\xba\xac\xe8\x88\xaa\xe7\xa9\xba\xe8\x88\xaa\xe5\xa4\xa9\xe5\xa4\xa7\xe5\xad\xa6'.decode())
b'hello world'
b'\xe5\x8c\x97\xe4\xba\xac\xe8\x88\xaa\xe7\xa9\xba\xe8\x88\xaa\xe5\xa4\xa9\xe5\xa4\xa7\xe5\xad\xa6'
b'\xe5\x8c\x97\xe4\xba\xac\xe8\x88\xaa\xe7\xa9\xba\xe8\x88\xaa\xe5\xa4\xa9\xe5\xa4\xa7\xe5\xad\xa6'
hello world
北京航空航天大学


2.格式化

a.旧式方法使用%连接

print('hello %s your final score is %lf'%('li ming',3.5))
C:\pythontest>python 1.py
hello li ming your final score is 3.500000
b.使用format()

format()将会依次替换{0} {1}所代表的值

print('hello {0} your final score is {1:.1f}'.format('li ming',3.5))
格式控制较为特殊,比较麻烦,但也比较强大

'{} {} {}'.format('a', 'b', 'c')





 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值