figuring out # of bytes

C

codecraig

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
 
J

Jaime Wyant

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

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
 
I

Irmen de Jong

codecraig said:
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

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

--Irmen
 
J

John J. Lee

Jaime Wyant said:
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
[...]

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


John
 
C

codecraig

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

thanks
 
J

Jaime Wyant

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.

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

jw
 
F

Fredrik Lundh

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

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>
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top