python 字节字符串_Python字符串转换为字节,字节转换为字符串
python 字节字符串
Python字符串到字节 (Python String to bytes)
Python String to bytes conversion can be done in two ways:
Python字符串到字节的转换可以通过两种方式完成:
- Using bytes() constructor and passing string and encoding as argument.使用bytes()构造函数并将字符串和编码作为参数传递。
- Using encode() method on string object.在字符串对象上使用encode()方法。
Python字节到字符串 (Python bytes to String)
We can convert bytes to String using bytes class decode() instance method.
我们可以使用bytes类的decode()实例方法将字节转换为String。
Let’s look at examples of converting a string to bytes and then bytes to string in a python program.
让我们看一下在python程序中将字符串转换为字节然后将字节转换为字符串的示例。
s = 'abc'# string to bytes using bytes()
b = bytes(s, encoding='utf-8')print(type(b))
print(b)# bytes to string using decode()
s = b.decode()
print('Original String =', s)s = 'xyz'# string to bytes using encode()
b = s.encode(encoding='utf-8')
print(b)s = b.decode()
print('Original String =', s)
Output:
输出:
<class 'bytes'>
b'abc'
Original String = abc
b'xyz'
Original String = xyz
将字符串转换为字节的最佳方法 (Best way to convert a string to bytes)
Both the ways to convert a string to bytes are perfectly fine. String encode() and decode() method provides symmetry whereas bytes() constructor is more object-oriented and readable approach. You can choose any of them based on your preference.
将字符串转换为字节的两种方法都很好。 字符串encode()和decode()方法提供了对称性,而bytes()构造函数则更面向对象且更易读。 您可以根据自己的喜好选择其中任何一个。
Reference: str.encode() api doc, bytes.decode() api doc
参考: str.encode()api文档 , bytes.decode()api文档
翻译自: https://www.journaldev.com/23500/python-string-to-bytes-to-string
python 字节字符串
python 字节字符串_Python字符串转换为字节,字节转换为字符串相关推荐
- jquery 逗号分割截取字符串_Python中常用的8种字符串操作方法
一.拼接字符串 使用"+"可以对多个字符串进行拼接 语法格式: str1 + str2 >>> str1 = "aaa">>> ...
- python按字节读取_Python read函数按字节(字符)读取文件的实现
文件对象提供了 read() 方法来按字节或字符读取文件内容,到底是读取宇节还是字符,则取决于是否使用了 b 模式,如果使用了 b 模式,则每次读取一个字节:如果没有使用 b 模式,则每次读取一个字符 ...
- python十六进制转换为字符串_Python 十六进制整数与ASCii编码字符串相互转换方法...
在使用Pyserial与STM32进行通讯时,遇到了需要将十六进制整数以Ascii码编码的字符串进行发送并且将接收到的Ascii码编码的字符串转换成十六进制整型的问题.查阅网上的资料后,均没有符合要求 ...
- python索引字符串_Python:通过索引删除子字符串
我有以下相当简单的片段: def delete_substring_blocks(s, blocks): ''' s: original input string blocks: list of in ...
- python逆序输出字符串_python实现对指定输入的字符串逆序输出的方法
这篇文章主要介绍了python实现对指定输入的字符串逆序输出的6种方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 对于一个给定的字符串,逆序输出,这个任务对于python来说是一种很简单的操作 ...
- python随机生成字符串_Python 2.6中的随机字符串(可以吗?)
我一直在试图找到一种更像python的方法来生成python中的随机字符串,这种方法也可以伸缩.通常,我看到类似的东西''.join(random.choice(string.letters) for ...
- python输入多行字符串_python中怎么输入多行字符串
Python中输入多行字符串: 方法一:使用三引号>>> str1 = '''Le vent se lève, il faut tenter de vivre. 起风了,唯有努力生存 ...
- python中如何将数字改成字符串_python中如何将数字转字符串
python数字转字符串的实现方法:1.使用格式化字符串,语句如"tt=322 tem='%d' %tt":2.通过"str(5)"方法实现转换. 数字转成字符 ...
- python 序列化压缩成字符串_Python 将json序列化后的字符串转换成字典(推荐)
一般而言下面的就可以完成需求了. def convertToDic(data): jsonDic=json.loads(data) return dict(jsonDic) 但实际应用中可能会出现一些 ...
- python数字转字符串_python中如何将数字转字符串
python数字转字符串的实现方法:1.使用格式化字符串,语句如"tt=322 tem='%d' %tt":2.通过"str(5)"方法实现转换. 推荐:< ...
最新文章
- python批量读取文件内容_Python 文本文件内容批量抽取实例
- 深度学习框架大PK:TNN决战MNN,ncnn依旧经典
- 解决网络请求的依赖关系
- employee setup in Organization unit
- php小程序群发通知,小程序实现群发功能代码的实现
- post多个参数_关于HTTP GET和POST的区别
- EasyOcr 安装(linux、docker)、使用(gin、python)
- 如何使用Java进行简单爬虫
- 【Linux】查询 OS、CPU、内存、硬盘信息
- mysql 分区 range_Mysql分区|mysql分区表|mysql partition|by range
- java-pdf转word
- 购书网站前端实现(HTML+CSS+JavaScript)
- Spark实践的阶段性总结
- Vue:成功解决[Vue warn]: Failed to resolve directive: modle (found in Anonymous)
- HTML与CSS3的知识整理
- 深度强化学习落地方法论(7)——训练篇
- through mysql.sock_MySQL server through socket '/tmp/mysql.sock' (2)
- 【数据可视化应用】Python反距离权重(IDW)插值计算及可视化绘制
- 未来的智能制造,或许会往这些方向推进
- htb inject
热门文章
- 木其工作室(专业程序代写服务)[转]学写块设备驱动(三)----踢开IO调度器,自己处理bio(下)...
- 第十八章 35用重载比较运算符实现字符串的比较
- Csdn Blog 开发团队致广大网友的一封信
- python 将一个列表切割成随机指定长度的多个列表
- [转载] python中异常处理的四个句子_Python学习笔记总结(四)异常处理
- [转载] 【python】定义带参数的装饰器,用装饰器限制函数的参数类型
- FPGA基础知识之主要的FPGA生产厂商介绍
- Xshell连接VMware的linux系统
- SVN更新无数次后仍显示Out of date
- 码农人生-如何快速使用从未学过的语言实现项目开发-第002期博文