473,396 Members | 1,997 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,396 software developers and data experts.

String length ... len(str)

Can someone please explain the following to me:
str1 = "here"
str2 = "here", "we", "are"
len(str1) 4 len(str2)

3

Why is len(str1) = 4 and len(str2) = 3?

If we were to say it's because the len() functions returns the number of elements in a string, then
len(str1) should be = 1

It's a little confusing.

Thanks,
Jeff
Jul 18 '05 #1
7 3860
> Why is len(str1) = 4 and len(str2) = 3?

Because str1 is a string whereas str2 is a tuple.
Both are sequences whose size is returned by len().
You probably meant str2 = "here" + "we" + "are"
If you ask "type(str2)" at the python prompt, you'll
see exactly what's going on.

Cheers,

Bernard.

Jul 18 '05 #2
Jeff Wagner wrote:
Can someone please explain the following to me:
str1 = "here"
str2 = "here", "we", "are"
len(str1)

4

len(str2)

3

Why is len(str1) = 4 and len(str2) = 3?

If we were to say it's because the len() functions returns the number of elements in a string, then
len(str1) should be = 1

It's a little confusing.


str2 =is a tuple of three strings, not a concatenated string. So you
are getting the number if strings in the tuple, not the number of
characters in a longer string. You have to use join to concatenate the
strings with either join() or +
str2 = "here", "we", "are"

str2 ('here', 'we', 'are') type (str2) <type 'tuple'> len (''.join (str2)) 9 str3 = "here" + "we" +"are"
str3 'hereweare' len (str3)

9

Jul 18 '05 #3

"Bernard Delmée" <bd*****@advalvas.REMOVEME.be> wrote in message
news:3f**********************@feed0.news.be.easyne t.net...
Why is len(str1) = 4 and len(str2) = 3?
Because str1 is a string whereas str2 is a tuple.
Both are sequences whose size is returned by len().
You probably meant str2 = "here" + "we" + "are"
If you ask "type(str2)" at the python prompt, you'll
see exactly what's going on.


Actually, you could also have said:

str2 = "here" "we" "are"

and gotten the concatination. Python automatically
concatinates string literals that come together. That
makes it a bit easier to split a literal across lines.
Admittedly, it's not the most obvious behavior,
although I believe it's faster since it happens in the
lexer (or the parser, but anyway at compile time).

John Roth
Cheers,

Bernard.

Jul 18 '05 #4
Thanks for the info, it clears up a lot of confusion for me.

I was under the impression that:
str1 = "here" + "we" + "are" was the same as
str2 = "here" , "we" , "are" ...

the only difference being that the str2 was separated by a space. But str1 is a string, str2 is a
tuple. Cool ... who ever came up with the word tuple? That's a weird name.

I think this clears up a few other things I had questions with, too.

Thanks a lot,
Jeff
Jul 18 '05 #5
Jeff Wagner wrote:
Thanks for the info, it clears up a lot of confusion for me.

I was under the impression that:
str1 = "here" + "we" + "are" was the same as
str2 = "here" , "we" , "are" ...

the only difference being that the str2 was separated by a space. But
str1 is a string, str2 is a
tuple.
Right. What probably confused you is that print acts like this:
print "one", "two", "three"

one two three

But this is only because print is a statement, and uses this as a
special form.
Cool ... who ever came up with the word tuple? That's a weird name.


It's derived from mathematics. A pair of items is a double, three items
is a triple, four is a quadruple, and n is an n-tuple.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \
\__/ Can I be your friend / 'Till the end
-- India Arie
Jul 18 '05 #6
Jeff Wagner <JW*****@hotmail.com> schreef:
Cool ... who ever came up with the word tuple? That's a weird name.


I'm not sure but it may come from words like "quintuple"...

--
JanC

"Be strict when sending and tolerant when receiving."
RFC 1958 - Architectural Principles of the Internet - section 3.9
Jul 18 '05 #7
JanC fed this fish to the penguins on Wednesday 03 December 2003 20:44
pm:


Jeff Wagner <JW*****@hotmail.com> schreef:
Cool ... who ever came up with the word tuple? That's a weird name.
I'm not sure but it may come from words like "quintuple"...

And is heavily used in formal relational database theory where it
equates to what most would call a "row" or even "record".

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Bestiaria Home Page: http://www.beastie.dm.net/ <
Home Page: http://www.dm.net/~wulfraed/ <


Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Steve | last post by:
Hi Guys, I have a string which contains data elements separated by spaces. I also have a function which returns the number of characters from the beginning of the string for a given number of...
21
by: google | last post by:
I'm trying to implement something that would speed up data entry. I'd like to be able to take a string, and increment ONLY the right-most numerical characters by one. The type structure of the...
17
by: Olivier Bellemare | last post by:
I've tried to make a function that returns the middle of a string. For example: strmid("this is a text",6,4); would return "is a". Here is my code: char *strmid(char *texte, int depart,...
4
by: ad | last post by:
I want to split a string into string by every a number. for example ss="abcdefghi; split(ss,3) --> "abc", "def","ghi" Are there this string function?
10
by: zahy[dot]bnaya[At]gmail[dot]com | last post by:
Hi, I am trying to come up with a c style string reverser, I want it to take 1 argument Altough I would never do this in real life. Is there a way to do it? I wrote this function that fails : ...
3
by: gxs | last post by:
I'm creating a program to convert a .dat file from English to pig Latin under the following rules: 1. If the word begins with a vowel, add "-way" to the end. 2.If the word doesn't begin with a...
10
by: Dancefire | last post by:
Hi, everyone, I'm writing a program using wstring(wchar_t) as internal string. The problem is raised when I convert the multibyte char set string with different encoding to wstring(which is...
7
by: samelzoro | last post by:
here is a problem in recursion: unexpected result ? by this program I just want to convert xml dom's document object to xml-string. (for all browsers) //load a xml function...
1
by: maestro | last post by:
If isMult is slow then: if len(str(a)) == len(str(r)) and isMult(a, r): trues.append((a, r)) will be much faster than: if isMult(a, r) and len(str(a)) == len(str(r)): trues.append((a, r))
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.