Help Me
Help Me

Reputation: 23

How do I replace a character in a string that repeats many times with a different character in python?

How do I replace a character in a string that repeats many times with a different character in python?

My code has a massive table in it that looks like this:

   A B C D E F G H I J

 1 - - - - - - - - - -

 2 - - - - - - - - - -

 3 - - - - - - - - - -

 4 - - - - - - - - - -

 5 - - - - - - - - - -

 6 - - - - - - - - - -

 7 - - - - - - - - - -

 8 - - - - - - - - - -

 9 - - - - - - - - - -

10 - - - - - - - - - -

I want it so that it will replace a "-" with a "X". Does anyone know how to do this?

Upvotes: 2

Views: 99

Answers (2)

mk8efz
mk8efz

Reputation: 1424

You can parse over the text and do a if/else check - my answer will be a general answer on how to approach the problem -

There are many ways to parse strings, but the "split" method is often useful:

http://www.tutorialspoint.com/python/string_split.htm

From there, you can loop through the text and do a check for your character:

 for txt in strings:
     if txt == '-':
         # execute your code here
     else:
         # else statement here

Upvotes: 0

user94559
user94559

Reputation: 60143

Just use str.replace:

text = text.replace('-', 'X')

Upvotes: 2

Related Questions