#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Changing a character in a string


    Hi,

    How would you change a character in a string.

    from: string = "cat"

    to: string = "cot"

    thanks
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,855
    Rep Power
    481
    I'd convert the string to a list, make the changes, then convert back to a string.
    Code:
    >>> s = list('cat')
    >>> s
    ['c', 'a', 't']
    >>> s[1] = 'o'
    >>> ''.join(s)
    'cot'
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Originally Posted by Dzung
    Hi,

    How would you change a character in a string.

    from: string = "cat"

    to: string = "cot"

    thanks
    To elaborate on b's reply, you technically can't do what you posted, since strings in Python are immutable: they cannot be "changed." However, lists are mutable, so you can take a sting apart into a list, change the list, and put it back together into a different string. You can even use the old variable name (`string' in your example) to refer to the new string, so it's essentially as if you had changed the string. (But not completely: if there were other references to the original string in your program, they will still refer to the old string.)
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0
    Originally Posted by b49P23TIvg
    I'd convert the string to a list, make the changes, then convert back to a string.
    Code:
    >>> s = list('cat')
    >>> s
    ['c', 'a', 't']
    >>> s[1] = 'o'
    >>> ''.join(s)
    'cot'
    >>>
    In your example, why is ''.join(s), even needed? Or I should say what is the purpose of adding it?

    thanks!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Originally Posted by spiralbrain
    In your example, why is ''.join(s), even needed? Or I should say what is the purpose of adding it?

    thanks!
    ['c', 'o', 't'] is a list. ''.join(['c', 'o', 't']) is a string. This is the third part of the procedure I stated in my post (putting the list back together into a string).
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0
    Originally Posted by Lux Perpetua
    ['c', 'o', 't'] is a list. ''.join(['c', 'o', 't']) is a string. This is the third part of the procedure I stated in my post (putting the list back together into a string).
    Got it.

IMN logo majestic logo threadwatch logo seochat tools logo