- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between a string and a byte string in Python?
A string is a sequence of characters; these are an abstract concept, and can't be directly stored on disk. A byte string is a sequence of bytes - things that can be stored on disk. The mapping between them is an encoding - there are quite a lot of these (and infinitely many are possible) - and you need to know which applies in the particular case in order to do the conversion, since a different encoding may map the same bytes to a different string. For example, the same byte string can represent 2 different strings in 2 different encodings.
For example
>>> b'\xcf\x84o\xcf\x81\xce\xbdo\xcf\x82'.decode('utf-16') '蓏콯캁澽苏' >>> b'\xcf\x84o\xcf\x81\xce\xbdo\xcf\x82'.decode('utf-8') 'τoρνoς'
Once you know which encoding to use, you can use the .decode() method of the byte string to get the right character string from it. The .encode() method of a character string goes the opposite way and encodes the character string as a byte string.
- Related Articles
- What is the difference between a String object and a String literal in Java?
- What is the difference between String and string in C#?
- What is the difference between parseInt(string) and Number(string) in JavaScript?
- What is the difference between a mutable and immutable string in C#?
- What is the difference between a String object and a StringBuffer object in java?
- What is the difference between String s1 = "Hello" and String s1= new String("Hello") in java?
- The Byte Objects vs String in Python
- What is the difference between character literals and string literals in Java?
- Difference between String buffer and String builder in Java
- Convert a String to a byte number in Java
- Difference between String and StringBuffer.
- What is the difference between a python module and a python package?
- What is the difference between String, StringBuffer and StringBuilder classes in Java explain briefly?
- Difference between String and StringBuilder in C#
- Difference between string and StringBuffer in Java.
