Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 27th, 2012, 07:32 PM
Dzung Dzung is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 2 Dzung User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old October 27th, 2012, 07:52 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is online now
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,391 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 15 h 5 m 5 sec
Reputation Power: 383
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!

Reply With Quote
  #3  
Old October 27th, 2012, 10:13 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,936 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 sec
Reputation Power: 1312
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.)

Reply With Quote
  #4  
Old October 29th, 2012, 10:45 PM
spiralbrain spiralbrain is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 2 spiralbrain User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #5  
Old October 30th, 2012, 02:29 AM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,936 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 sec
Reputation Power: 1312
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).

Reply With Quote
  #6  
Old October 30th, 2012, 03:47 PM
spiralbrain spiralbrain is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 2 spiralbrain User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Changing a character in a string

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap