string methods

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

    string methods

    I am an abject newbie, so mock away (actually no-one ever does that in
    this group..)

    Anyway, I want to replace one character in a string, based in that
    character's position in the string.

    For example if I wanted to replace the 4th character in 'foobar' (the
    b)with the contents of another string, newchar, what would be the
    easiest way?

    I know this touches on immutability etc, but I can't find string methods
    to return the first 3 characters, and then the last 2 characters, which
    I could concatenate with newchar to make a new string.

    I know the string methods are there, but can't find it in any docs, and
    just want to check the syntax, unless there is an easier way.

    Thanks.
  • tiissa

    #2
    Re: string methods

    anthonyberet wrote:[color=blue]
    > I know this touches on immutability etc, but I can't find string methods
    > to return the first 3 characters, and then the last 2 characters, which
    > I could concatenate with newchar to make a new string.
    >
    > I know the string methods are there, but can't find it in any docs, and
    > just want to check the syntax, unless there is an easier way.[/color]

    Strings [1] are sequences [2], and therefore support slicing, which is
    what you are looking for.


    [1] http://docs.python.org/lib/string-methods.html
    [2] http://docs.python.org/lib/typesseq.html

    Comment

    • Brian Beck

      #3
      Re: string methods

      anthonyberet wrote:[color=blue]
      > I know this touches on immutability etc, but I can't find string methods
      > to return the first 3 characters, and then the last 2 characters, which
      > I could concatenate with newchar to make a new string.[/color]

      As tiissa said, you want slicing:

      py> s = "foobar"
      py> s[:3]
      'foo'
      py> s[:3] + "B" + s[4:]
      'fooBar'
      py>

      --
      Brian Beck
      Adventurer of the First Order

      Comment

      • Peter Hansen

        #4
        Re: string methods

        Brian Beck wrote:[color=blue]
        > anthonyberet wrote:[color=green]
        >>I know this touches on immutability etc, but I can't find string methods
        >>to return the first 3 characters, and then the last 2 characters, which
        >>I could concatenate with newchar to make a new string.[/color]
        >
        > As tiissa said, you want slicing:
        >
        > py> s = "foobar"
        > py> s[:3]
        > 'foo'
        > py> s[:3] + "B" + s[4:]
        > 'fooBar'[/color]

        And if that's too ugly for you and you think you need to do this
        operation a lot, just define a function to do it for you based on the
        index value and string that you pass in to it.

        -Peter

        Comment

        • Dennis Lee Bieber

          #5
          Re: string methods

          On Sat, 30 Jul 2005 16:15:10 +0100, anthonyberet <nospam@me.inva lid>
          declaimed the following in comp.lang.pytho n:

          [color=blue]
          >
          > For example if I wanted to replace the 4th character in 'foobar' (the
          > b)with the contents of another string, newchar, what would be the
          > easiest way?[/color]
          [color=blue][color=green][color=darkred]
          >>> s = "foobar"
          >>> a = "another"
          >>> n = s[:3] + a + s[4:]
          >>> n[/color][/color][/color]
          'fooanotherar'[color=blue][color=green][color=darkred]
          >>>[/color][/color][/color]

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

          Comment

          • Martin v. Löwis

            #6
            Re: string methods

            anthonyberet wrote:[color=blue]
            > For example if I wanted to replace the 4th character in 'foobar' (the
            > b)with the contents of another string, newchar, what would be the
            > easiest way?[/color]

            Depends on how your input is specified. If you know it is the b you
            want to replace, you write
            [color=blue][color=green][color=darkred]
            >>> text="foobar"
            >>> text = text.replace("b ","baz")
            >>> text[/color][/color][/color]
            'foobazar'

            There is no issue with immutability here: .replace returns a new
            string object, and you assign this to the text variable (thus dropping
            the reference to the string "foobar").

            If you know it is the fourth character you want to replace, you
            do as people have suggested:
            [color=blue][color=green][color=darkred]
            >>> text="foobar"
            >>> text=text[:3]+"baz"+text[4:]
            >>> text[/color][/color][/color]
            'foobazar'

            And, if you know in advance that the string is "foobar", and that
            it is the fourth character, and that the replacement string is "baz",
            you write
            [color=blue][color=green][color=darkred]
            >>> text="foobazar"[/color][/color][/color]

            :-)

            Regards,
            Martin

            Comment

            Working...