- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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.
>>> 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.
- 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 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 a python module and a python package?
- What is the difference between a python list and a tuple?
- What is the difference between a python tuple and a dictionary?
- What is the difference between character literals and string literals in Java?
- The Byte Objects vs String in Python
- Difference between String and StringBuffer.
- What is the difference between String s1 = "Hello" and String s1= new String("Hello") in java?
- Difference between String buffer and String builder in Java
- What is the difference between a python list and an array?