Skip to content
Fix Code Error

Best way to convert string to bytes in Python 3?

March 13, 2021 by Code Error
Posted By: Anonymous

There appear to be two different ways to convert a string to bytes, as seen in the answers to TypeError: 'str' does not support the buffer interface

Which of these methods would be better or more Pythonic? Or is it just a matter of personal preference?

b = bytes(mystring, 'utf-8')

b = mystring.encode('utf-8')

Solution

If you look at the docs for bytes, it points you to bytearray:

bytearray([source[, encoding[, errors]]])

Return a new array of bytes. The bytearray type is a mutable sequence of integers in the range 0 <= x < 256. It has most of the usual methods of mutable sequences, described in Mutable Sequence Types, as well as most methods that the bytes type has, see Bytes and Byte Array Methods.

The optional source parameter can be used to initialize the array in a few different ways:

If it is a string, you must also give the encoding (and optionally, errors) parameters; bytearray() then converts the string to bytes using str.encode().

If it is an integer, the array will have that size and will be initialized with null bytes.

If it is an object conforming to the buffer interface, a read-only buffer of the object will be used to initialize the bytes array.

If it is an iterable, it must be an iterable of integers in the range 0 <= x < 256, which are used as the initial contents of the array.

Without an argument, an array of size 0 is created.

So bytes can do much more than just encode a string. It’s Pythonic that it would allow you to call the constructor with any type of source parameter that makes sense.

For encoding a string, I think that some_string.encode(encoding) is more Pythonic than using the constructor, because it is the most self documenting — "take this string and encode it with this encoding" is clearer than bytes(some_string, encoding) — there is no explicit verb when you use the constructor.

Edit: I checked the Python source. If you pass a unicode string to bytes using CPython, it calls PyUnicode_AsEncodedString, which is the implementation of encode; so you’re just skipping a level of indirection if you call encode yourself.

Also, see Serdalis’ comment — unicode_string.encode(encoding) is also more Pythonic because its inverse is byte_string.decode(encoding) and symmetry is nice.

Answered By: Anonymous

Related Articles

  • Is CSS Turing complete?
  • Can't install via pip because of egg_info error
  • Best way to extract messy HTML tables using BeautifulSoup
  • Examples of GoF Design Patterns in Java's core libraries
  • Keras Sequential API is replacing every layer with…
  • Possible to use a let within an if/cond?
  • What's the difference between eval, exec, and compile?
  • Changing PowerShell's default output encoding to UTF-8
  • How to create range in Swift?
  • Replacing a 32-bit loop counter with 64-bit…
  • How does PHP 'foreach' actually work?
  • problem with client server unix domain stream…
  • How to get UTF-8 working in Java webapps?
  • Validate that a string is a positive integer
  • using d3.js with aurelia framework
  • Using StringWriter for XML Serialization
  • Search match multiple values in single field in…
  • Compression/Decompression string with C#
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • What are type hints in Python 3.5?
  • What encoding/code page is cmd.exe using?
  • How to remove white space characters from a string…
  • How to check if a string contains text from an array…
  • Python 3 TypeError: must be str, not bytes with…
  • Smart way to truncate long strings
  • How do I merge two dictionaries in a single…
  • u'ufeff' in Python string
  • Why does C++ code for testing the Collatz conjecture…
  • laravel vuejs/axios put request Formdata is empty
  • Why is "1000000000000000 in range(1000000000000001)"…
  • Return char[256] from an accessor of a char[256] attribute
  • How does String substring work in Swift
  • CUDA: Pass device function as an argument to global function
  • Usage of __slots__?
  • Python str vs unicode types
  • npm install error in vue
  • For-each over an array in JavaScript
  • What's the best way to get the last element of an…
  • How to generate a random string of a fixed length in Go?
  • Change column type in pandas
  • Reversing a string in C
  • Flatten points into SVG polyline Polymer
  • What is Python buffer type for?
  • What is the difference between UTF-8 and Unicode?
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • Converting between strings and ArrayBuffers
  • How to generate a random number in C++?
  • Getting "Abort trap 6" using memset()
  • Usage of unicode() and encode() functions in Python
  • Can't understand the difference between declaring a…
  • How to update Python?
  • Filling a calendar using Arrayformula or LOOKUP
  • Representing null in JSON
  • Python mmap.mmap() to bytes-like object?
  • How to bundle and import buffer npm package with…
  • libiconv: Safe estimation of target bytes-length…
  • When I'm testing a web app by JUnit and Mockito I…
  • Compiler error: "class, interface, or enum expected"
  • How to use java.net.URLConnection to fire and handle…
  • Python - bytearray function with string literal
  • How to filter a RecyclerView with a SearchView
  • Using Javascript's atob to decode base64 doesn't…
  • python encoding utf-8
  • How to resolve "Could not find schema information…
  • Single loss with Multiple output model in TF.Keras
  • What are the new features in C++17?
  • Convert Unicode to ASCII without errors in Python
  • Byte Array to Hex String
  • How to convert a UTF-8 string into Unicode?
  • Which are the advantages of byte objects over string…
  • How to make a SIMPLE C++ Makefile
  • Editing ZIP archive in place in Golang
  • Convert UTF-8 with BOM to UTF-8 with no BOM in Python
  • What are the undocumented features and limitations…
  • aurelia-cli error when using buffer.js - global undefined
  • Unexpected end of JSON input while parsing
  • How to avoid using Select in Excel VBA
  • How to Remove Line Break in String
  • Encrypting & Decrypting a String in C#
  • What does Ruby have that Python doesn't, and vice versa?
  • WAVE file unexpected behaviour
  • Behavior of program changing based on the order of…
  • Vue JS - How do I get WebGL to render via Vue…
  • How can I determine whether a 2D Point is within a Polygon?
  • Start redis-server with config file
  • v-if not triggered on computed property
  • Reference - What does this regex mean?
  • Best way to combine two or more byte arrays in C#
  • Fastest way to iterate over all the chars in a String
  • UnsatisfiedDependencyException: Error creating bean…
  • SVG path with stroke fills the svg element
  • RE error: illegal byte sequence on Mac OS X
  • Creating a singleton in Python
  • Update points Redux React Native
  • Split string in C every white space
  • Improved way to get subsets of a list
  • How to convert networkx node positions to…
  • org.springframework.beans.factory.NoSuchBeanDefiniti…
  • Install pip in docker
  • Use of Jquery on scroll event

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Generate random number between two numbers in JavaScript

Next Post:

Writing files in Node.js

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error