What does 'b' refers to here. If it is some sort of special character to inculde ',' in string then why the ouput [b'1',b'2',b'3'] include b?

>>> b'1,2,3'.split(b',')
[b'1', b'2', b'3']
>>> b'1,2,3'.split(b',', maxsplit=1)
[b'1', b'2,3']
>>> b'1,2,,3,'.split(b',')
[b'1', b'2', b'', b'3', b'']

I found this:


Maybe this will answer your question.
1 Like

I am completely understood what does b means in front of num, string. I am wondering why there is need of byte type data and what is its use case?

I don’t know which lesson or which exercise you are on and in which course are you studying this. So I cannot explain for what it is being used there but I can tell you why we need byte type data.
Byte type data is very essential as it is the only way by which programming languages communicate with the system to generate output. All of your code is converted to bytes. Also, if you study Computer Science, you will learn about data structures and how they function. There you need these kind of byte type data to perform tasks. For instance,
In data structures called Hash Maps (called dictionaries in python),
we generate hash_code and indexes using the byte type data. If you have learnt about dictionaries in python, you would be knowing that data is stored in a dictionary as a key value pair where one single key cannot store two different data. This kind of data structures is created using Hash Maps where we convert the key to byte type data, then we sum up all of its bytes to generate hash_code. Next we take the remainder or dividing hash_code by the length of that array(dictionary for you). We get an index position where we can store that value of the key.

This all helps in making new programming languages, creating OS, working with machines on the binary level, understanding how things work on the binary level, and much more.
If you are interested in this stuff you might want to take the Computer Science career path on codecademy.

Does byte data occupy a single byte in memory? Or is it apportioned more memory than that?

For instance, values greater than 128 must be escaped, and text is confined to ASCII, as I understand it. One does not mind being wrong so spell it out, please.

ASCII at its most basic was only six bits, as I recall. This level of miniaturization must go to some level of compression technique, also, as a sidebar?

Bytes literals are always prefixed with ‘b’ or ‘B’; they produce an instance of the bytes type instead of the str type. They may only contain ASCII characters; bytes with a numeric value of 128 or greater must be expressed with escapes.

This is explanation of type of print(type(b’string’)). My question here is what is the use case of using byte literals If it is in python there is some use case

I find this byte literals when I was reading python documentation under split function they are using in example. This the code on python docs

>>> b'1,2,3'.split(b',')
[b'1', b'2', b'3']
>>> b'1,2,3'.split(b',', maxsplit=1)
[b'1', b'2,3']
>>> b'1,2,,3,'.split(b',')
[b'1', b'2', b'', b'3', b'']

Okay, that wasn’t something I was trying to make you understand. Let’s take this a bit deeper.
When we encode any literal into bytes we get a list and to encode we have a method for strings called .encode(). For example,
If I try to encode ‘dog’ to byte form I would use type 'dog'.encode(). This gives me output as b'dog'. This output is actually a list of bytes for each single character, i.e, ‘d’, ‘o’ and ‘g’. Encode has two parameters. One is the encoding which is set to by default to ‘utf-8’. So if I use sum() function on b'dog' it would return me the sum of all the bytes for each single character in that string literal, which is also called it’s hash_code.

And about the use of these bytes in python or any other language I think I already gave a detailed answer where we can use these byte type data, which is, in computer science.
I hope this time I am able to solve your doubts.

1 Like