Reputation: 23
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
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