String length ... len(str)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff Wagner

    String length ... len(str)

    Can someone please explain the following to me:
    [color=blue][color=green][color=darkred]
    >>> str1 = "here"
    >>> str2 = "here", "we", "are"
    >>> len(str1)[/color][/color][/color]
    4[color=blue][color=green][color=darkred]
    >>> len(str2)[/color][/color][/color]
    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
  • Bernard Delmée

    #2
    Re: String length ... len(str)

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

    Comment

    • Jay O'Connor

      #3
      Re: String length ... len(str)

      Jeff Wagner wrote:
      [color=blue]
      >Can someone please explain the following to me:
      >
      >
      >[color=green][color=darkred]
      >>>>str1 = "here"
      >>>>str2 = "here", "we", "are"
      >>>>len(str1)
      >>>>
      >>>>[/color][/color]
      >4
      >
      >[color=green][color=darkred]
      >>>>len(str2)
      >>>>
      >>>>[/color][/color]
      >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.
      >
      >[/color]

      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 +
      [color=blue][color=green][color=darkred]
      >>> str2 = "here", "we", "are"
      >>>
      >>> str2[/color][/color][/color]
      ('here', 'we', 'are')[color=blue][color=green][color=darkred]
      >>> type (str2)[/color][/color][/color]
      <type 'tuple'>[color=blue][color=green][color=darkred]
      >>> len (''.join (str2))[/color][/color][/color]
      9[color=blue][color=green][color=darkred]
      >>> str3 = "here" + "we" +"are"
      >>> str3[/color][/color][/color]
      'hereweare'[color=blue][color=green][color=darkred]
      >>> len (str3)[/color][/color][/color]
      9

      Comment

      • John Roth

        #4
        Re: String length ... len(str)


        "Bernard Delmée" <bdelmee@advalv as.REMOVEME.be> wrote in message
        news:3fce7c40$0 $1127$6c56d894@ feed0.news.be.e asynet.net...[color=blue][color=green]
        > > Why is len(str1) = 4 and len(str2) = 3?[/color]
        >
        > 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.[/color]

        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[color=blue]
        >
        > Cheers,
        >
        > Bernard.
        >[/color]


        Comment

        • Jeff Wagner

          #5
          Re: String length ... len(str)

          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


          Comment

          • Erik Max Francis

            #6
            Re: String length ... len(str)

            Jeff Wagner wrote:
            [color=blue]
            > 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.[/color]

            Right. What probably confused you is that print acts like this:
            [color=blue][color=green][color=darkred]
            >>> print "one", "two", "three"[/color][/color][/color]
            one two three

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

            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 && max@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

            Comment

            • JanC

              #7
              Re: String length ... len(str)

              Jeff Wagner <JWagner@hotmai l.com> schreef:
              [color=blue]
              > Cool ... who ever came up with the word tuple? That's a weird name.[/color]

              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

              Comment

              • Dennis Lee Bieber

                #8
                Re: String length ... len(str)

                JanC fed this fish to the penguins on Wednesday 03 December 2003 20:44
                pm:
                [color=blue]
                >
                >
                > Jeff Wagner <JWagner@hotmai l.com> schreef:
                >[color=green]
                >> Cool ... who ever came up with the word tuple? That's a weird name.[/color]
                >
                > I'm not sure but it may come from words like "quintuple" ...
                >[/color]
                And is heavily used in formal relational database theory where it
                equates to what most would call a "row" or even "record".

                --[color=blue]
                > =============== =============== =============== =============== == <
                > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
                > wulfraed@dm.net | Bestiaria Support Staff <
                > =============== =============== =============== =============== == <
                > Bestiaria Home Page: http://www.beastie.dm.net/ <
                > Home Page: http://www.dm.net/~wulfraed/ <[/color]

                Comment

                Working...