By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,523 Members | 1,737 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,523 IT Pros & Developers. It's quick & easy.

figuring out # of bytes

P: n/a
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

Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 22 Apr 2005 13:28:57 -0700, codecraig <co*******@gmail.com> wrote:
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
Jul 19 '05 #2

P: n/a
codecraig wrote:
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
Jul 19 '05 #3

P: n/a
Jaime Wyant <pr***********@gmail.com> writes:
On 22 Apr 2005 13:28:57 -0700, codecraig <co*******@gmail.com> wrote:
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

[...]

That's only an issue if type(x) != str. (eg., type(x) == unicode)
John
Jul 19 '05 #4

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

thanks

Jul 19 '05 #5

P: n/a
On 4/22/05, Jaime Wyant <pr***********@gmail.com> wrote:
On 22 Apr 2005 13:28:57 -0700, codecraig <co*******@gmail.com> wrote:
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.


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

jw
Jul 19 '05 #6

P: n/a
"codecraig" wrote:
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>

Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.