String question

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

    String question

    I want to split a sentence and assign each word to a variable.
    In Ruby I can do it as:

    v1,v2,v3,v4,v5 = str1.split

    Which will be the Python equivalent ? Thanks.

    Andrew.
  • Tim Golden

    #2
    Re: String question

    Andreu wrote:
    I want to split a sentence and assign each word to a variable.
    In Ruby I can do it as:
    >
    v1,v2,v3,v4,v5 = str1.split
    >
    Which will be the Python equivalent ? Thanks.
    That would be:

    str1 = "The quick brown fox jumps"
    v1, v2, v3, v4, v5 = str1.split ()

    TJG

    Comment

    • Andreu

      #3
      Re: String question

      Wow...about ten seconds to get a kind response ....
      Thanks Tim.

      Andrew.

      Tim Golden wrote:
      Andreu wrote:
      >I want to split a sentence and assign each word to a variable.
      >In Ruby I can do it as:
      >>
      >v1,v2,v3,v4, v5 = str1.split
      >>
      >Which will be the Python equivalent ? Thanks.
      >
      That would be:
      >
      str1 = "The quick brown fox jumps"
      v1, v2, v3, v4, v5 = str1.split ()
      >
      TJG

      Comment

      • cokofreedom@gmail.com

        #4
        Re: String question

        On Jun 23, 4:45 pm, Andreu <r...@sys1.orgw rote:
        I want to split a sentence and assign each word to a variable.
        In Ruby I can do it as:
        >
        v1,v2,v3,v4,v5 = str1.split
        >
        Which will be the Python equivalent ? Thanks.
        >
        Andrew.
        Well a straight copy would be...
        >>example = "Hello, how are you"
        >>v1, v2, v3, v4 = example.split()
        >>print v1, v2, v3, v4
        Hello, how are you
        >>print v1
        Hello,

        However I would make a list of it.
        >>v_list = example.split()
        >>print v_list
        ['Hello,', 'how', 'are', 'you']
        >>for word in v_list:
        print word
        Hello,
        how
        are
        you

        That seems to me to be the more pythonic way to do it, since it is
        dynamic.

        Comment

        • Andreu

          #5
          Re: String question

          Yes, ... don't ask me why, but in fact v1,v2,v3 = str1.split()
          does not seem to work. My original problem was I forgot about
          the parenthesis as Tim point out. So I ended up converting to a
          list as in: v = str1.split() and accessing the elements using
          v[0] v[1] ect...it is working now. Thanks.

          Andreu.


          cokofreedom@gma il.com wrote:
          However I would make a list of it.
          >>>v_list = example.split()
          That seems to me to be the more pythonic way to do it, since it is
          dynamic.

          Comment

          • Mark Tolonen

            #6
            Re: String question


            "Andreu" <root@sys1.orgw rote in message news:g3p72i$432 $1@aioe.org...
            Yes, ... don't ask me why, but in fact v1,v2,v3 = str1.split()
            does not seem to work. My original problem was I forgot about
            the parenthesis as Tim point out. So I ended up converting to a
            list as in: v = str1.split() and accessing the elements using
            v[0] v[1] ect...it is working now. Thanks.
            >
            Andreu.
            v1,v2,v3 = str1.split() will only work if there are exactly three things in
            str1.
            >>s = 'this is a test'
            >>v1,v2,v3 = s.split()
            Traceback (most recent call last):
            File "<interacti ve input>", line 1, in <module>
            ValueError: too many values to unpack
            >>v1,v2,v3 = s.split(' ',2) # limit to two splits maximum
            >>v1
            'this'
            >>v2
            'is'
            >>v3
            'a test'

            -Mark

            Comment

            • cokofreedom@gmail.com

              #7
              Re: String question

              On Jun 24, 5:38 am, "Mark Tolonen" <M8R-yft...@mailinat or.comwrote:
              "Andreu" <r...@sys1.orgw rote in messagenews:g3p 72i$432$1@aioe. org...
              Yes, ... don't ask me why, but in fact v1,v2,v3 = str1.split()
              does not seem to work. My original problem was I forgot about
              the parenthesis as Tim point out. So I ended up converting to a
              list as in: v = str1.split() and accessing the elements using
              v[0] v[1] ect...it is working now. Thanks.
              >
              Andreu.
              >
              v1,v2,v3 = str1.split() will only work if there are exactly three things in
              str1.
              >
              >s = 'this is a test'
              >v1,v2,v3 = s.split()
              >
              Traceback (most recent call last):
              File "<interacti ve input>", line 1, in <module>
              ValueError: too many values to unpack>>v1,v2,v 3 = s.split(' ',2) # limit to two splits maximum
              >v1
              'this'
              >v2
              'is'
              >v3
              >
              'a test'
              >
              -Mark
              In Python 3k I believe you can put a * next to one of the variables to
              hold multiple arguments. That'll be aidful!

              Comment

              • Terry Reedy

                #8
                Re: String question

                cokofreedom@gma il.com wrote:
                On Jun 24, 5:38 am, "Mark Tolonen" <M8R-yft...@mailinat or.comwrote:
                In Python 3k I believe you can put a * next to one of the variables to
                hold multiple arguments. That'll be aidful!
                IDLE 3.0b1
                >>a,b,*c=[1,2,3,4,5]
                >>c
                [3, 4, 5]
                >>a,*b,c = [1,2,3,4,5]
                >>b
                [2, 3, 4]
                >>a,b,*c=[1,2]
                >>c
                []

                Augmented assignment is quite similar to argument passing:
                at most one starred target;
                at least as many items as un-starred targets (as now).

                tjr

                Comment

                Working...