String length ... len(str)

John Roth newsgroups at jhrothjr.com
Wed Dec 3 19:58:06 EST 2003


"Bernard Delmée" <bdelmee at advalvas.REMOVEME.be> wrote in message
news:3fce7c40$0$1127$6c56d894 at feed0.news.be.easynet.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.
>






More information about the Python-list mailing list