manipulate string

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

    manipulate string

    I heard that strings are immutable, but isn't there regardless a way to
    manipulate a string?
    I have a string that looks like this:
    a = '0123456789'
    But I want it to look like this:
    a = '0 - 2 - 4 - 6 - 8 - '
    I want whitespace between every number and I want to fade out every
    second number.
    It is funny that I can explicitly read parts of that string, print a[3]
    works perfectly, but I can't remove it, delete it or what ever.

    I would appreciate any hint on how to solve my little "problem".

    Thanks a lot.
    Regards, Tom

  • Michel Claveau/Hamster

    #2
    Re: manipulate string

    Like this :


    import string

    a='0123456789'
    l=list(a)
    b=string.join(l ,' - ')
    print b



    @-salutations
    --
    Michel Claveau
    mél : http://cerbermail.com/?6J1TthIa8B



    Comment

    • Eric Brunel

      #3
      Re: manipulate string

      Michel Claveau/Hamster wrote:[color=blue]
      > Like this :
      >
      >
      > import string
      >
      > a='0123456789'
      > l=list(a)
      > b=string.join(l ,' - ')
      > print b[/color]

      Apparently, the OP wants to discard characters at indexes 1, 3, 5, 7, etc... So,
      assuming Python version is 2.3, the correct solution seems to be:
      [color=blue][color=green][color=darkred]
      >>> a='0123456789'
      >>> print ' - '.join(a[::2])[/color][/color][/color]
      0 - 2 - 4 - 6 - 8

      HTH
      --
      - Eric Brunel <eric dot brunel at pragmadev dot com> -
      PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

      Comment

      • Michel Claveau/Hamster

        #4
        Re: manipulate string

        Oups ! Sorry, i had read too fast...




        Comment

        • Michel Claveau/Hamster

          #5
          Re: manipulate string

          2 by 2 :


          import string
          a='0123456789'
          l=list(a[::2])
          b=string.join(l ,' - ')
          print b


          Comment

          • Chris

            #6
            Re: manipulate string

            Thanks guys.
            I knew it couldn't be too difficult, but I just didn't know in which
            direction I should search for a solution. :-)

            Thanks again.
            Regards, Tom

            Comment

            • Chris

              #7
              Re: manipulate string

              It is me again.
              Unfortunately it doesn't work. Python doesn't accept [::2]
              TypeError: sequence index must be integer
              I use Python Release 2.2.3.
              Any ideas?

              -Tom

              Comment

              • David C. Fox

                #8
                Re: manipulate string

                > import string[color=blue]
                > a='0123456789'
                > l=list(a[::2])
                > b=string.join(l ,' - ')
                > print b[/color]

                Chris wrote:
                [color=blue]
                > It is me again.
                > Unfortunately it doesn't work. Python doesn't accept [::2]
                > TypeError: sequence index must be integer
                > I use Python Release 2.2.3.
                > Any ideas?
                >
                > -Tom
                >[/color]

                Yeah, the x[::i] notation for strings and lists only works in 2.3.

                A couple of alternatives to l = list(a[::2]) which work in 2.2 are:

                l = []
                for i in range( (len(a) + 1) // 2):
                l.append(a[2*i])

                or more compactly

                l=[a[2*i] for i in range((len(a)+1 )//2)]

                or

                l = [a[i] for i in range(len(a)) if 2*(i//2) == i]

                David

                Comment

                • David C. Fox

                  #9
                  Re: manipulate string

                  David C. Fox wrote:
                  [color=blue][color=green]
                  >> import string
                  >> a='0123456789'
                  >> l=list(a[::2])
                  >> b=string.join(l ,' - ')
                  >> print b[/color]
                  >
                  >
                  > Chris wrote:
                  >[color=green]
                  >> It is me again.
                  >> Unfortunately it doesn't work. Python doesn't accept [::2]
                  >> TypeError: sequence index must be integer
                  >> I use Python Release 2.2.3.
                  >> Any ideas?
                  >>
                  >> -Tom
                  >>[/color]
                  >
                  > Yeah, the x[::i] notation for strings and lists only works in 2.3.
                  >
                  > A couple of alternatives to l = list(a[::2]) which work in 2.2 are:
                  >
                  > l = []
                  > for i in range( (len(a) + 1) // 2):
                  > l.append(a[2*i])
                  >
                  > or more compactly
                  >
                  > l=[a[2*i] for i in range((len(a)+1 )//2)]
                  >
                  > or
                  >
                  > l = [a[i] for i in range(len(a)) if 2*(i//2) == i]
                  >
                  > David
                  >[/color]

                  or

                  l = [a[i] for i in range(0, len(a), 2)]

                  David

                  Comment

                  • Werner Schiendl

                    #10
                    Re: manipulate string

                    Hi,

                    Eric Brunel wrote:[color=blue]
                    >
                    > Apparently, the OP wants to discard characters at indexes 1, 3, 5, 7,
                    > etc... So, assuming Python version is 2.3, the correct solution seems to
                    > be:
                    >[color=green][color=darkred]
                    > >>> a='0123456789'
                    > >>> print ' - '.join(a[::2])[/color][/color]
                    > 0 - 2 - 4 - 6 - 8
                    >[/color]

                    almost :-)

                    the original poster said he wanted

                    a = '0 - 2 - 4 - 6 - 8 - '

                    which means the last faded-out 9 is missing in your solution


                    the following works:
                    [color=blue][color=green][color=darkred]
                    >>> a = '0123456789'
                    >>>
                    >>> b = " ".join( [i % 2 and "-" or a[i] for i in range(len(a))] )
                    >>> b[/color][/color][/color]
                    '0 - 2 - 4 - 6 - 8 -'


                    I think the trailing space is a typo of the OP...


                    hth

                    Werner

                    Comment

                    • Werner Schiendl

                      #11
                      Re: manipulate string

                      Hi,

                      Werner Schiendl wrote:
                      [color=blue]
                      >
                      > the following works:
                      >[color=green][color=darkred]
                      > >>> a = '0123456789'
                      > >>>
                      > >>> b = " ".join( [i % 2 and "-" or a[i] for i in range(len(a))] )
                      > >>> b[/color][/color]
                      > '0 - 2 - 4 - 6 - 8 -'
                      >
                      >[/color]

                      if you prefer to use generators, you could use:
                      [color=blue][color=green][color=darkred]
                      >>> a = '0123456789'
                      >>>
                      >>> def hide_every_2nd( x):[/color][/color][/color]
                      .... while True:
                      .... yield x.next()
                      .... x.next()
                      .... yield "-"
                      ....[color=blue][color=green][color=darkred]
                      >>> b = " ".join(hide_eve ry_2nd(iter(a)) )
                      >>> b[/color][/color][/color]
                      '0 - 2 - 4 - 6 - 8 -'


                      hth

                      Werner

                      Comment

                      • Inyeol Lee

                        #12
                        Re: manipulate string

                        On Tue, Oct 28, 2003 at 07:06:36PM +0100, Chris wrote:[color=blue]
                        > It is me again.
                        > Unfortunately it doesn't work. Python doesn't accept [::2]
                        > TypeError: sequence index must be integer
                        > I use Python Release 2.2.3.
                        > Any ideas?[/color]

                        Then,
                        [color=blue][color=green][color=darkred]
                        >>> s = "0123456789 "
                        >>> " - ".join([s[i] for i in range(0, 10, 2)])[/color][/color][/color]
                        '0 - 2 - 4 - 6 - 8'

                        -Inyeol

                        Comment

                        • Chris

                          #13
                          Re: manipulate string

                          Thanks for all the help.
                          I just decided to update to Python 2.3 anyway.

                          Thanks to all, Tom

                          Comment

                          • Skip Montanaro

                            #14
                            Re: manipulate string

                            [color=blue][color=green][color=darkred]
                            >>> s = "0123456789 "
                            >>> " - ".join([s[i] for i in range(0, 10, 2)])[/color][/color][/color]
                            '0 - 2 - 4 - 6 - 8'

                            I thought the original poster wanted the odd numbers replaced by '-' and then
                            all elements separated by spaces. None of the ' - '.join(...) examples
                            achieve that, as they fail to replace '9' with '-'. Here's an alternative
                            which does:
                            [color=blue][color=green][color=darkred]
                            >>> s = "0123456789 "
                            >>> [int(c)%2 and '-' or c for c in s][/color][/color][/color]
                            ['0', '-', '2', '-', '4', '-', '6', '-', '8', '-'][color=blue][color=green][color=darkred]
                            >>> ' '.join([int(c)%2 and '-' or c for c in s])[/color][/color][/color]
                            '0 - 2 - 4 - 6 - 8 -'

                            Skip

                            Comment

                            • Michel Claveau/Hamster

                              #15
                              Re: manipulate string

                              LOL



                              Comment

                              Working...