String length ... len(str)

J

Jeff Wagner

Can someone please explain the following to me:
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
 
?

=?ISO-8859-1?Q?Bernard_Delm=E9e?=

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.
 
J

Jay O'Connor

Jeff said:
Can someone please explain the following to me:



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 +
9
 
J

John Roth

Bernard Delmée said:
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
 
J

Jeff Wagner

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
 
E

Erik Max Francis

Jeff said:
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:
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 && (e-mail address removed) && 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
 
D

Dennis Lee Bieber

JanC fed this fish to the penguins on Wednesday 03 December 2003 20:44
pm:
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".

--
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top