473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How do you replace a character in a string?

just wondering how to replace a space (' ') in a string to a dot ('.')

so far i have this...

Expand|Select|Wrap|Line Numbers
  1. def add_dots(game):
  2.        for row in game:
  3.         for char in row:
  4.             if char == ' ':
  5.  
thanks
Apr 13 '10 #1
6 28114
bvdet
2,851 Expert Mod 2GB
Since strings are immutable, you must create a new string. Following are a couple of ways:
Expand|Select|Wrap|Line Numbers
  1. >>> s = 'abcdef ghijk'
  2. >>> s1 = s.replace(' ', '.')
  3. >>> s1
  4. 'abcdef.ghijk'
  5. >>> s2 = ''.join([[c,'.'][c == ' ' or 0] for c in s])
  6. >>> s2
  7. 'abcdef.ghijk'
  8. >>> 
Apr 13 '10 #2
@bvdet
hey, thanks for that. However i am using python IDLE... i can't use '.replace' i have to use range or enumerate....
Apr 14 '10 #3
bvdet
2,851 Expert Mod 2GB
The last time I checked, str.replace() worked in Idle. Did you notice the list comprehension in my previous post? You could do something like that.
Apr 14 '10 #4
Glenton
391 Expert 256MB
@pythondummy
So obviously the ways that @bvdet suggested are superior, but if you really want to do it the way you suggested, then do the following (by the way, the indentation in your code was wrong - you need to be careful with python! The standard is 4 spaces for a tab, but any number is okay as long as it's consistent)

Expand|Select|Wrap|Line Numbers
  1. def add_dots(game):
  2.     newgame=[]
  3.     for row in game:
  4.         newrow=""
  5.         for char in row:
  6.             if char == ' ':
  7.                 newrow+="."
  8.             else:
  9.                 newrow+=char
  10.         newgame.append(newrow)
  11.     return newgame
I haven't tried it, so I'll leave it as an exercise to debug. If you're still stuck, just post back.

We'd also appreciate it if you post back how you've done good or bad!
Apr 14 '10 #5
@Glenton
haha, all good... i figured it out finally. i used enumerate
Apr 14 '10 #6
bvdet
2,851 Expert Mod 2GB
Here's a couple of ways using a for loop:
Expand|Select|Wrap|Line Numbers
  1. >>> s
  2. 'abcdef ghijk'
  3. >>> sList = []
  4. >>> for letter in s:
  5. ...     if letter == " ":
  6. ...         sList.append(".")
  7. ...     else:
  8. ...         sList.append(letter)
  9. ...         
  10. >>> "".join(sList)
  11. 'abcdef.ghijk'
  12. >>> s
  13. 'abcdef ghijk'
  14. >>> s1 = ''
  15. >>> for letter in s:
  16. ...     if letter == " ":
  17. ...         s1 += "."
  18. ...     else:
  19. ...         s1 += letter
  20. ...         
  21. >>> s1
  22. 'abcdef.ghijk'
  23. >>> 
Apr 14 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Billy N. Patton | last post by:
-------- Original Message -------- Subject: <string>.replace Date: Fri, 15 Oct 2004 11:07:19 -0500 From: Billy N. Patton <b-patton@ti.com> Organization: Texas Instruments Newsgroups:...
19
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not...
14
by: Etu | last post by:
Hi, I have a string: string c = "'abc' \"cde\", 'mno' \"xyz\","; how can I use the c.Replace(???, ???) method to have this string: "'abc' "cde", 'mno' "xyz"," that is, all the...
5
by: djc | last post by:
I need to prepare a large text database field to display in an asp.net repeater control. Currently I am replacing all chr(13)'s with a "<br/>" and it works fine. However, now I also want to be able...
3
by: Pascal | last post by:
bonjour hello I would like to trim a string of all its white spaces so i used myString.trim() but it doesn't work as supposed : unsecable space are remaining in the middle of my string... i...
8
by: Warren Moxley | last post by:
Hi there, i've been searching for a C String search and replace function. I need to find all occurrences of " " in a char* array, and replace them with another char, I know how to do this in...
5
by: herman | last post by:
How can I replace all occurrences of a character with another character in std string? For example, I want to replace '/' with '+' in my std::string I have looked at the replace() method in...
6
by: Gabriel | last post by:
Hello, I do this : s = Environment.CurrentDirectory; s = s.Replace("\\", @"\"); Environment.CurrentDirectiry return a path like this C:\\....\\....\\..... I'd like replace the \\ by \, but...
10
by: Lonifasiko | last post by:
Hi, Just want to replace character at index 1 of a string with another character. Just want to replace character at that position. I thought Replace method would be overloaded with an index...
5
by: shapper | last post by:
Hello, I have a text as follows: "My email is something@something.xyz and I posted this @ 2 am" I need to replace the @ by (AT) bu only the ones that are in email addresses. All other @...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.