编程语言
首页 > 编程语言> > Python字符串

Python字符串

作者:互联网

在这里插入图片描述

在这里插入图片描述

print(ord('c'))   #获取c的位置
print(chr(99))    #获取99对应的字符

运行结果:

99
c

编码与解码过程、

s1='可口可乐'
print(s1.encode('utf-8'))  #编码过程

s1_encode=b'\xe5\x8f\xaf\xe5\x8f\xa3\xe5\x8f\xaf\xe4\xb9\x90'
print(s1_encode.decode('utf-8'))         #解码过程

运行结果:

b'\xe5\x8f\xaf\xe5\x8f\xa3\xe5\x8f\xaf\xe4\xb9\x90'
可口可乐
print(bytes('百','utf-8'))  #把百字用utf-8的形式 转换成字节
print(bytes('a','ascii'))   #把a字母用ascii的形式 转换成字节

运行结果:

b'\xe7\x99\xbe'
b'a'

二、str bytes bytearray
str是字符数据,bytes和bytearray是字节数据。它们都是序列,可以进行迭代遍历。
str和bytes是不可变序列,bytearray是可变序列,可以原处修改字节。

b=bytearray('白','utf-8') #白字转换之前用utf-8形式编码
b.append(92)              #可对b进行类似字符串的操作
print(b.decode('utf-8'))

运行结果:

白\
qq_41782791 发布了12 篇原创文章 · 获赞 0 · 访问量 173 私信 关注

标签:bytearray,utf,xe5,Python,bytes,print,x8f,字符串
来源: https://blog.csdn.net/qq_41782791/article/details/104199208