473,385 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

figuring out # of bytes

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
6 1120
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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nathan Pinno | last post by:
Hi all, I need help figuring out how to fix my code. I'm using Python 2.2.3, and it keeps telling me invalid syntax in the if name == "Nathan" line. Here is the code if you need it. #This...
10
by: Kristian Nybo | last post by:
Hi, I'm writing a simple image file exporter as part of a school project. To implement my image format of choice I need to work with big-endian bytes, where 'byte' of course means '8 bits', not...
2
by: Roberto Leibman | last post by:
I haven't been able to figure this out, or find any resource for this. I'd like to be able to know, in javascript, if the media that's rendering is screen or print. I have no nefarious purpose...
19
by: Lorenzo J. Lucchini | last post by:
My code contains this declaration: : typedef union { : word Word; : struct { : byte Low; : byte High; : } Bytes; : } reg;
8
by: ranjeet.gupta | last post by:
Dear All I am not able o understand the exact number of bytes allocation done by the two fucntion given below, It is said that the fuction Condition_String1 allocates the 240 bytes while...
1
by: Darin Browne | last post by:
We figure budgets in 26 14 days periods in a year. I currently can take a date and calculate which of those 26 periods that date falls in. Now I have to figure out what is beginning and ending...
1
by: | last post by:
one thing to keep in mind is that i've written multiple tcp apps in .net 2002 and 2003, and haven't seen this problem before. I have a simple socket app where a client sends some # of bytes to...
0
by: you | last post by:
Hiya! I am having some trouble figuring out how to use reflection in a specific way and was wondering if anyone could help. I hope that I can explain what I am trying to do clearly so bear with...
5
by: philip | last post by:
Here is some lines of code than I wrote. You can copy/paste theis code as code of form1 in a new project. My problem is this one : I try to write in a file a serie of bytes. BUT some bytes...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.