Home » Python

What does the 'b' character do in front of a string literal in Python?

Here, we are going to learn how to make a variable type as byte – we will also learn what does the 'b' character do in front of a string literal in Python?
Submitted by Sapna Deraje Radhakrishna, on February 01, 2020

Consider the following examples,

# variable declarations
test_str = 'string'
test_bytes = b'string'

# printing the types


<class 'str'>
<class 'bytes'>

As per the above example, the prefix of 'b' character to a string, makes the variable of type bytes.

Before version 3, python always ignored the prefix 'b' and in the later version, bytes variable are always prefixed with ‘b’. They may contain ASCII characters, bytes with a numeric value of 128 or greater must be expressed with escapes.

The bytes are the actual data. Strings are an abstraction.

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.