For a simpler solution you could instead use rjust on the last 4 characters of the string, and fill it with # up to its original length:

s = 'TestName'
s[-4:].rjust(len(s), '#')

'####Name'

The problem with your function, is that you have to repeat the elements you want to use to replace as many times as replacements there will be. So you should do:

def maskify(cc):
    c2 = cc.replace(cc[:-4], '#'*len(cc[:-4]))
    return c2
Answer from yatu on Stack Overflow
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-string-replace-character-at-specific-position
Python - Replace Character at Specific Index in String
Python - Replace character at given index - To replace a character with a given character at a specified index, you can use python string slicing; or convert the string to list, replace and then back to string.
๐ŸŒ
thisPointer
thispointer.com โ€บ home โ€บ python โ€บ python: replace character in string by index position
Python: Replace character in string by index position - thisPointer
November 3, 2020 - That means if the given index position for replacement is greater than the number of characters in a string, then it can give unexpected results. Therefore we should always check if the given nth position is in the range or not. Find and replace string values in a List in Python
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-multiple-indices-replace-in-string
Multiple Indices Replace in String โ€“ Python | GeeksforGeeks
January 17, 2025 - For loop iterates through the indices provided in li and for each index the string is updated by replacing the character at that specific index. ... In Python, replacing multiple lines in a file consists of updating specific contents within a text file. This can be done using various modules and their associated functions.
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ replace-character-string-python
Python Replace Character in String | FavTutor
October 6, 2021 - Slicing is a method in python which ... lists, and tuples. Using slicing, you can return a range of characters by specifying the start index and end index separated by a colon and return the part of the string....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-replace-to-k-at-ith-index-in-string
Replace a String character at given index in Python - GeeksforGeeks
April 16, 2025 - In Python, strings are immutable, meaning they cannot be directly modified. We need to create a new string using various methods to replace a character at a specific index.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-remove-index-ranges-from-string
Python - Remove index ranges from String - GeeksforGeeks
May 3, 2023 - In this, we check for each range, and remake string, considering the index doesn't lie in range checking using conditional statements. ... # Python3 code to demonstrate working of # Remove index ranges from String # Using loop # initializing strings test_str1 = 'geeksforgeeks is best for geeks' # printing original string print("The original string 1 is : " + str(test_str1)) # initializing ranges list range_list = [(3, 6), (7, 10), (14, 17)] res = "" for idx, chr in enumerate(test_str1): for strt_idx, end_idx in range_list: # checking for ranges and appending if strt_idx <= idx + 1 <= end_idx: break else: res += chr # printing result print("The reconstructed string : " + str(res))
Find elsewhere
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ python-program-to-replace-a-character-at-a-specific-index
Python program to change character of a string using given index
July 11, 2023 - def change_multiple_characters(s, changes): char_list = list(s) for index, char in changes.items(): if 0 <= index < len(char_list): char_list[index] = char return ''.join(char_list) # Example usage s = "python" changes = {1: 'Y', 3: 'P', 5: 'N'} result = change_multiple_characters(s, changes) print(f"Original: {s}") print(f"Modified: {result}") ... Use string slicing for single character replacement and list conversion for multiple changes.
๐ŸŒ
TutorialsTonight
tutorialstonight.com โ€บ python-replace-character-in-string-by-index
Python Replace Character in String by Index
Using this we access the substring from index 0 to index-1 and concatenate it with the character and the substring from index+1 to the end of the string. ... # Method 2: # using string slicing def replace_char(str, index, char): # concatenate the substring from 0 to index-1 and character # and substring from index+1 to the end of the string str = str[:index] + char + str[index+1:] # return the string return str str = "tutorials tonight" # replace character at index 0 with 'T' print(replace_char(str, 0, 'T')) # replace character at index 10 with 'T' print(replace_char(str, 10, 'T'))
Top answer
1 of 2
2

There are two things you should do with this:

  • Create a function that prints the current playing field, given the cells. So, you can print it whenever you want.
  • Change the item of the cells list, which corresponds to the entered coordinates.

In the example below I used your code to look up the index of the cell you need to change by adding cellnum = coordinates.index(t_given_coordinates). This line defines a new integer variable that holds the cell number according to your map. You may then use this index to change the content of this cell and redraw the complete thing.

cells = ['-']*9

def print_matrix(cells):
    matrix = """
    ---------
    | {0} {1} {2} |
    | {3} {4} {5} |
    | {6} {7} {8} |
    ---------
    """.format(*cells)
    print(matrix)

coordinates = [
                ("1", "3"), ("2", "3"), ("3", "3"),
                ("1", "2"), ("2", "2"), ("3", "2"),
                ("1", "1"), ("2", "1"), ("3", "1")
]


print_matrix(cells)

end = False
while not end:
    try:
        given_coordinates = input("Enter the coordinates: ").split(", ")
        t_given_coordinates = tuple(given_coordinates)
        cellnum = coordinates.index(t_given_coordinates)
        cells[cellnum] = "X"
        print_matrix(cells)
    except:
        print("That was an invalid input")
        end = True


The output will look somewhat like that:

    ---------
    | - - - |
    | - - - |
    | - - - |
    ---------
    
Enter the coordinates: 1, 1

    ---------
    | - - - |
    | - - - |
    | X - - |
    ---------
    
Enter the coordinates: 2, 3

    ---------
    | - X - |
    | - - - |
    | X - - |
    ---------
    
Enter the coordinates: hello world
That was an invalid input

But, if I may suggest a slight change:

  • The initial filling of the cells could already be the cell indices. So, the user just has to enter the index of the next cell to fill, which is a single integer. That's much less prone to raise errors. So, when the field is empty, it just reads:
---------
| 0 1 2 |
| 3 4 5 |
| 6 7 8 |
---------
2 of 2
0

I would represent your choises as simple flat list and calculate the position inside this list from the tuples you get as inputs like so:

def print_list(l):
    """Takes a list of 9 elements and prints them in a 3x3 matrix
    replacing None with empty spaces."""
    matrix = """
---------
| {0} {1} {2} |
| {3} {4} {5} |
| {6} {7} {8} |
---------
"""

    print(matrix.format(*(what or " " for what in l)))


# use a list to store the taken fields
choices = ["X", None, None,
           "X", "O", "O",
            None, None, None]

print_list(choices)


c = ("3","1")  # the tuple your input gave you
# create numbers from it
c = list(map(int,c))
# calculate list position
pos = (c[0]-1)*3+c[1]-1
# fill correct letter in
choices [pos] = "X"

print_list(choices)

to get

---------
| X     |
| X O O |
|       |
--------- 

---------
| X     |
| X O O |
| X     |
---------

Obviously you would need to

  • check if the position in the list is still None to avoid overwriting X by O (or reverse)
  • change the letter to be written to X or O (whoever ones turn it is)
  • determine if a winning move was made
  • ...

See f.e. Search in duckduckgo.com for python + tic tac to on stackoverflow.com

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-program-to-replace-list-elements-within-a-range-with-a-given-number
Python Program to replace list elements within a range with a given number - GeeksforGeeks
July 23, 2025 - # initializing list test_list = [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1] # printing original list print("The original list is : " + str(test_list)) # initializing i, j i, j = 4, 8 # initializing K K = 9 # getting range using slicing and # required elements using * operator test_list[i:j] = [K] * (j - i) # printing result print("Range Updated list : " + str(test_list))
๐ŸŒ
ProgrammingBasic
programmingbasic.com โ€บ home โ€บ python โ€บ replace character in string by index in python
Replace character in String by index in Python | ProgrammingBasic
Here, we will discuss two ways to replace any character in a string at a specific index using : ... The list() and join() method. The string slicing method helps to return a range of characters from a string by slicing it.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 52409584 โ€บ replace-substring-of-given-indices-range
python - Replace substring of given indices range - Stack Overflow
You have the right idea, but you probably meant list(string) instead of string.split(), and you need the length of the '*' to match the length of what they're replacing in the slice assignment.
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
How to replace a specific index position in a list - Python Help - Discussions on Python.org
November 3, 2021 - Hello! So, just as the title says, how do I replace a specific index position? If you look at the bottom where it says blankword.replace(blankword[i],guess), that is what Iโ€™m having trouble with. I think I know why it doโ€ฆ