figuring out # of bytes

Discussion in 'Python' started by codecraig, Apr 22, 2005.

  1. codecraig

    codecraig Guest

    i want to the number of bytes in a string...

    is, len(x) accurate?

    so, x = "hi"
    len(x) == 2 ....so that means two bytes?

    thanks
     
    codecraig, Apr 22, 2005
    #1
    1. Advertisements

  2. codecraig

    Jaime Wyant Guest

    No, that means that the string is two bytes in length. The number of
    bytes is dependent on the encoding. It seems like there was a thread
    on this subject recently.

    jw
     
    Jaime Wyant, Apr 22, 2005
    #2
    1. Advertisements

  3. As long as type(x)==str, this is correct.

    --Irmen
     
    Irmen de Jong, Apr 22, 2005
    #3
  4. codecraig

    John J. Lee Guest

    [...]

    That's only an issue if type(x) != str. (eg., type(x) == unicode)


    John
     
    John J. Lee, Apr 24, 2005
    #4
  5. codecraig

    codecraig Guest

    so each character in the string is 1 byte? if so, can u point me to
    somewhere that states that perhaps?

    thanks
     
    codecraig, Apr 25, 2005
    #5
  6. codecraig

    Jaime Wyant Guest

    Ughh, i misstated. More correctly, the string is 2 "characters" in
    length. The number of bytes per character depends on the encoding.

    jw
     
    Jaime Wyant, Apr 25, 2005
    #6
  7. the documentation, perhaps?

    http://www.python.org/doc/ref/types.html

    Strings

    The items of a string are characters. There is no separate
    character type; a character is represented by a string of one
    item. Characters represent (at least) 8-bit bytes. The built-in
    functions chr() and ord() convert between characters and
    nonnegative integers representing the byte values. Bytes
    with the values 0-127 usually represent the corresponding
    ASCII values, but the interpretation of values is up to the
    program. The string data type is also used to represent
    arrays of bytes, e.g., to hold data read from a file.

    </F>
     
    Fredrik Lundh, Apr 25, 2005
    #7
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.