Here is the Python String program. It compares each string [item] with an entry from the user. And increases count. When the count equals 2, replace the string[item] with space. Nice program to practice.

Modify a string by replacing the second occurrence of a specified character with a space.

The program replaces second occurrence with the entry from the user when it matches.

str1 = "1A2A3ABBBB1111_KKKK"
print("Before modification: ", str1)

b = input("Enter Alpha :")
count = 0
str_back=""

for c in range(0, len(str1)):
if str1[c] == b:
# print(str1[c])
count += 1
if count == 2:
new_str = list(str1)
new_str[c] = " "
#print(new_str)
string_back = "".join(new_str)


print("After modified String :" , string_back)

This program takes an input string str1 and prompts the user to enter a value for b. It then iterates through each character in str1 and checks if it is equal to b.

If a character in str1 matches b, it is printed and a count is increased by 1. When the count reaches 2, the character at that position is removed from str1. It is replaced with an empty string.

Finally, the modified string str1 is printed. The program will print the characters in str1 that match b. It will also remove the second occurrence  b from str1.

Output

In this example, you can see that the letter ‘A’ becomes null after the number 2.

Before modification:  1A2A3ABBBB1111_KKKK
Enter Alpha :B
After modified String  : 1A2A3AB BB1111_KKKK