how to prepend string to a string?

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

    how to prepend string to a string?

    so I know you can append a string. But how do you *prepend* a string,
    as shown in the following code

    #dirList = ['depth1','depth 2','depth3']
    #string = """position """
    #for x in len(dirList):
    # string += '>> %s'%dirList.pop () #(????????????)
    #

    to return
    position>> depth1 >> depth2 >> depth3

    rather than
    position>> depth3 >> depth2 >> depth1


    I really hope this feature exists =)

    -thanks

  • Benji York

    #2
    Re: how to prepend string to a string?

    flamesrock wrote:[color=blue]
    > so I know you can append a string. But how do you *prepend* a string,
    > as shown in the following code
    >
    > #dirList = ['depth1','depth 2','depth3']
    > #string = """position """
    > #for x in len(dirList):
    > # string += '>> %s'%dirList.pop () #(????????????)
    > #[/color]

    How about string = whatever + string?

    Note that recommended form is to build a list of strings and then use
    ''.join(all_my_ strings) to form the final result.

    After saying all that, here's a better way:

    dirList = ['depth1','depth 2','depth3', 'position']
    print ' >> '.join(dirList)
    --
    Benji York

    Comment

    • Peter Hansen

      #3
      Re: how to prepend string to a string?

      flamesrock wrote:[color=blue]
      > so I know you can append a string. But how do you *prepend* a string,
      > as shown in the following code
      >
      > #dirList = ['depth1','depth 2','depth3']
      > #string = """position """
      > #for x in len(dirList):
      > # string += '>> %s'%dirList.pop () #(????????????)[/color]

      There's not _really_ anything wrong with this code, provided the length
      of dirList is "short" (since otherwise the performance issues of growing
      strings with += may cause trouble). It might also be preferable to
      change from the for statement to a "while dirList:" since that doesn't
      require the unused "x" and the confusion of apparently testing the
      length of a list that is getting shorter with each iteration.

      -Peter

      Comment

      • Kent Johnson

        #4
        Re: how to prepend string to a string?

        flamesrock wrote:[color=blue]
        > so I know you can append a string. But how do you *prepend* a string,
        > as shown in the following code
        >
        > #dirList = ['depth1','depth 2','depth3']
        > #string = """position """
        > #for x in len(dirList):
        > # string += '>> %s'%dirList.pop () #(????????????)
        > #
        >
        > to return
        > position>> depth1 >> depth2 >> depth3[/color]

        ISTM the problem is not prepending, but getting the elements of dirList in the order you want. If
        you iterate over the list normally you get the desired result:
        dirList = ['depth1','depth 2','depth3']
        string = """position """
        for x in dirList:
        string += '>> %s'% x

        Kent

        Comment

        • flamesrock

          #5
          Re: how to prepend string to a string?

          Thanks for all the responses :)

          You're right, Kent. That does exactly what I need it to.

          Anyways.. if there isn't a prepend function, I'm going to request it
          from the devs. Would be cool to see that in future versions of python.

          Comment

          • Peter Hansen

            #6
            Re: how to prepend string to a string?

            flamesrock wrote:[color=blue]
            > Thanks for all the responses :)
            >
            > You're right, Kent. That does exactly what I need it to.
            >
            > Anyways.. if there isn't a prepend function, I'm going to request it
            > from the devs. Would be cool to see that in future versions of python.[/color]

            Since strings cannot be modified in-place (they are "immutable" ), the
            concept of prepend for a string is meaningless (and so is append).
            Instead, you simply build a new string from other bits, and it's
            entirely up to you which comes first and which comes second (thus
            providing implementations equivalent to prepend or append, as you need).

            -Peter

            Comment

            • newcoder

              #7
              Re: how to prepend string to a string?

              How about this:

              dirList = ['depth1', 'depth2', 'depth3']
              string = """position """

              for x in range(len(dirLi st)):
              string += '>> %s' % dirList.pop(0)

              print string



              flamesrock wrote:[color=blue]
              > Thanks for all the responses :)
              >
              > You're right, Kent. That does exactly what I need it to.
              >
              > Anyways.. if there isn't a prepend function, I'm going to request it
              > from the devs. Would be cool to see that in future versions of python.[/color]

              Comment

              Working...