在Python3中将字符串转换为字节的最佳方法

To convert a string to bytes, there are more than one way,

要将字符串转换为字节,有多种方法,

Approach 1: use encode() method

方法1:使用encode()方法

test_str = "include_help"
print(type(test_str))
test_bytes = test_str.encode()
print(test_bytes)
print(type(test_bytes))

Output

输出量

<class 'str'>
b'include_help'
<class 'bytes'>

In python3, the first parameter to encode() defaults to 'utf-8'. This approach is also supposedly faster because the default argument results in NULL in the C code.

在python3中, encode()的第一个参数默认为'utf-8' 。 据推测,这种方法也更快,因为默认参数在C代码中导致NULL。

Approach 2: use bytes() constructor

方法2:使用bytes()构造函数

test_str = "include_help"
test_bytes_v2 = bytes(test_str, 'utf-8')
print(type(test_bytes_v2))
print(test_bytes_v2)

Output

输出量

<class 'bytes'>
b'include_help'

Using the bytes constructor gives more options than just encoding the string. However, for encoding a string the approach1, is more pythonic than using a constructor, because it is more readable.

使用bytes构造函数提供了更多的选项,而不仅仅是对字符串进行编码。 但是,与使用构造函数相比,对字符串1进行编码比使用Python1具有更多的Python风格,因为它更具可读性。

翻译自: https://www.includehelp.com/python/best-way-to-convert-string-to-bytes-in-python3.aspx

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/541841.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Java】PMD规则学习(1) --字符串比较

PMD是一款采用BSD协议发布的Java程序代码检查工具。该工具可以做到检查Java代码中是否含有未使用的变量、是否含有空的抓取块、是否含有不必要的对象等。该软件功能强大&#xff0c;扫描效率高&#xff0c;是Java程序员debug的好帮手。 PMD支持的编辑器包括&#xff1a;JDevelo…

php定义object数据类型,PHP数据类型(4):对象object

//创建一个类class Student{//定义属性public $name XuGZh;public $age 20;public $sex 男;//定义方法public function getInfo(){//当前对象中访问自己属性用伪变量$thisreturn my name:.$this->name..my age:.$this->age..my sex:.$this->sex;}}对象初始化:要创建…

avr计数_使用8位LCD创建计数器| AVR

avr计数This type of counter may be also used in the EVM machines. A counter can be used to count the number of times a button is pressed. It can have many applications. The most widely used counter application is in EVM and also in customer feedback machin…

php将字符变为数字,数字字符怎么转化为数字 php 怎么将字符转成数字

java中&#xff0c;String字符串转化为数字我现在想把一个String字符串转化为数字&#xff0c; String s"00000123" 我直接使java中String字符串转化为数字&#xff1a; 转换为浮点型&#xff1a; 使用Double或者Float的parseDouble或者parseFloat方法进行转换 Strin…

用U盘作为启动盘做系统步骤

步骤一&#xff1a;BIOS设置U盘启动 制作好Win10 U盘系统安装盘之后&#xff0c;我们需要在电脑的BIOS设置中把第一启动设备设置为U盘&#xff0c;设置后就可以从我们制作的Win10 U盘系统安装盘启动&#xff0c;从而显示系统安装界面开始安装系统。BIOS设置U盘启动的方法如下&a…

使用tkinter模块在Python中进行GUI编程

GUI (Graphical User Interface): GUI(图形用户界面)&#xff1a; GUI is a simple application which helps the user to interact with the computer or any other electronic device through a graphical icon. This used to perform different tasks on a desktop or lapt…

php轻博客社区视频教程,轻博客主题 - SEO极致优化的ZBLOG轻博客主题

zblog自适应轻博客主题&#xff0c;简洁、轻巧、极致优化~QQ群&#xff1a;457320274 (问题反馈以及其他链接交换等) 交流社区&#xff1a;https://www.bxiu.net/ (有问题可以求助交流)更新记录&#xff1a;2021.02.22 v2.8 更新内容&#xff1a;1、新增分类自定义标题&#xf…

Composer学习之————Ubuntu14.04下安装Composer

下载Composer&#xff1a; curl -sS https://getcomposer.org/installer | php 安装Composer&#xff1a; /usr/bin/php composer.phar --version 设置全局命令&#xff1a; sudo mv composer.phar /usr/local/bin/composer 查看是否安装与设置成功&#xff1a; composer -vers…

u盘启动iso 开源_启动和维护开源项目

u盘启动iso 开源Lets talk about how to start an open-source project? The process can be classified as in three phases, 让我们谈谈如何启动一个开源项目&#xff1f; 该过程可以分为三个阶段&#xff0c; Individual senses the need of the project: This is the pha…

java如何解决高并发症,JAVA线上故障紧急处理详细过程!

链接&#xff1a;https://fredal.xin/java-error-check?hmsrtoutiao.io&utm_mediumtoutiao.io&utm_sourcetoutiao.io线上故障主要会包括 CPU、磁盘、内存以及网络问题&#xff0c;而大多数故障可能会包含不止一个层面的问题&#xff0c;所以进行排查时候尽量四个方面依…

程序员如何谈加薪?

如果你对现在公司很满意&#xff0c;只是觉得薪资太低&#xff0c;那么可以先和你的主管聊聊。 首先&#xff0c;讲一讲自己最近在工作上的成长&#xff0c;看主管是否认同&#xff1b; 然后&#xff0c;从能力提升角度&#xff0c;向主管要一个更大的发展空间和更大的业务挑战…

php有多少魔术方法,PHP常用的几个魔术方法

常用的魔术方法有&#xff1a;__Tostring () __Call() __autoLoad() __ clone() __GET() __SET() __isset() __unset()1.__Tostring()用于定义输出对象引用时调用常用于打印一些对象的信息必须有返回值eg&#xff1a;有一个persion类Persion per new persion()Echo per; //直接…

python常用语法和示例_使用Python中的示例进行输入和输出操作

python常用语法和示例A Program needs to interact with the user to accomplish the desired task; this is done using Input-Output facility. Input means the data entered by the user of the program. In python, we have input() and raw_input ( ) function available…

关于node.js和npm 和nvm_byKL

关于node.js和npm 和nvm Node 是一个服务器端 JavaScript 解释器&#xff0c;Node 本身运行 V8 JavaScript。V8 JavaScript 引擎是 Google 用于其 Chrome 浏览器的底层 JavaScript 引擎。 NPM是随同NodeJS一起安装的包管理工具&#xff0c;能解决NodeJS代码部署上的很多问题&am…

php 查看扩展 代码,[扩展推荐] 使用 PHP Insights 在终端查看 PHP 项目代码质量

PHP Insights 是一个由 Nuno Maduro 发布的、可在控制台进行 PHP 即时质量检查的拓展包。在项目的 readme 文件中&#xff0c;可以发现 PHP Insights 的主要功能包含&#xff1a;代码质量 与 代码风格 分析一个针对于代码 结构 和 复杂度 的漂亮的预览界面在 Laravel、Symfon…

航空机票预订c#代码_航空公司座位预订问题的C ++程序

航空机票预订c#代码Problem statement: Write a program to assign passengers seats in an airplane. Assume a small airplane with seat numbering as follows: 问题陈述&#xff1a;编写一个程序来分配飞机上的乘客座位。 假设小型飞机的座位编号如下&#xff1a; 1 A B C…

linux命令之which

which这个命令可以说并不常用&#xff0c;它的作用是查看可执行文件的位置&#xff0c;并返回第一个搜索结果。可执行文件也就是指的某个系统命令&#xff0c;但是这个命令的位置必须是在PATH路径里存在的。截图中 &#xff0c;pwd的位置在/bin/pwd,当然&#xff0c;这个路径是…

线性代数向量乘法_向量的标量乘法| 使用Python的线性代数

线性代数向量乘法Prerequisite: Linear Algebra | Defining a Vector 先决条件&#xff1a; 线性代数| 定义向量 Linear algebra is the branch of mathematics concerning linear equations by using vector spaces and through matrices. In other words, a vector is a mat…

sonar扫描普通JAVA执行,SonarQube扫描源代码的方法

SonarQube扫描源代码的方法雷建锋一、分析源代码综述一旦成功安装了SonarQube平台&#xff0c;您就可以开始安装一个分析器并开始创建项目了。在第一次分析时&#xff0c;该平台会自动创建一个项目。如果您需要在第一个分析之前在项目上设置一些配置&#xff0c;那么您可以选择…

html的学习思维导图

转载于:https://www.cnblogs.com/lingdublog/p/6438088.html