[Python] What is the diffrence between a byte string and a normal string in python?

Discussão em 'Python' iniciado por Stack, Julho 18, 2025.

  1. Stack

    Stack Membro Participativo

    I have gone through dozens of articles, and this StackOverflow thread but I'm still as clueless as ever. The main points I have read everywhere and why I don't understand them are:

    The main points I have read everywhere and why I don't understand them are:

    Byte string string What I dont understand
    It is a sequence of bytes It is a sequence of Unicode characters Everything in the memory and disc is just sequence of bytes. How does this make one different? Since Unicode characters are themselves bytes
    Used to handle binary data Used to handle textual date Don't even understand what this mean
    Not human readable Human readable Not sure what this is, even the 1's and 0's are human readable


    Demonstrating that both types of data are identically stored
    s = "Hello"
    b = b"Hello"

    with open("str.txt", "w") as f:
    f.write(s)

    with open("bytes.txt", "wb") as f:
    f.write(b)



    A hexdump reveals it to be true

    C:\Users\OM>hexdump.exe bytes.txt
    000000 48 65 6c 6c 6f


    C:\Users\OM>hexdump str.txt
    000000 48 65 6c 6c 6f


    Further, After a little talk with ChatGPT, I was able to get it to say that "The only difference in both types is that strings, unlike bytes, store metadata for its characters. But the metadata is stored only in memory. And after a system reboot, the stored string and byte string has no difference at all."
    I assume its hallucinating but if its true, then I dont see any reason whatsoever why the existence of both data types doesn't make each other useless and obsolete.

    Continue reading...

Compartilhe esta Página