The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Changing a character in a string
Discuss Changing a character in a string in the Python Programming forum on Dev Shed. Changing a character in a string Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 27th, 2012, 07:32 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 2
Time spent in forums: 5 m 31 sec
Reputation 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
|

October 27th, 2012, 07:52 PM
|
 |
Contributing User
|
|
|
|
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!
|

October 27th, 2012, 10:13 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
Quote: | 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.)
|

October 29th, 2012, 10:45 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 2
Time spent in forums: 43 m 8 sec
Reputation Power: 0
|
|
Quote: | 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!
|

October 30th, 2012, 02:29 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
Quote: | 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).
|

October 30th, 2012, 03:47 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 2
Time spent in forums: 43 m 8 sec
Reputation Power: 0
|
|
Quote: | 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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|