0

I have the following python string:

game="""+-------+-------+-------+
|       |       |       |
|   1   |   2   |   3   |
|       |       |       |
+-------+-------+-------+
|       |       |       |
|   4   |   5   |   6   |
|       |       |       |
+-------+-------+-------+
|       |       |       |
|   7   |   8   |   9   |
|       |       |       |
+-------+-------+-------+
"""

This is considered a str still and I used the following to replace "5" with "X":

game.replace("5","X")

Then I print the output and still get:

+-------+-------+-------+
|       |       |       |
|   1   |   2   |   3   |
|       |       |       |
+-------+-------+-------+
|       |       |       |
|   4   |   5   |   6   |
|       |       |       |
+-------+-------+-------+
|       |       |       |
|   7   |   8   |   9   |
|       |       |       |
+-------+-------+-------+

2 Answers2

2

What you are probably doing is

game="sample string 5"
game.replace("5", "X")

This wont work because python strings are immutable and .replace() doesn't modify the original string, it returns a new one

To fix:

game = game.replace("5, "X")

A little word about what it means for Strings to be immutable in python - it simply means that they cannot change during the runtime of the program.

CherylLamb
  • 21
  • 2
1

I think you are printing the wrong string. You need to print game.replace('5','X') directly or use an intermediate variable.

samuelnehool
  • 147
  • 15