0

My mission is to get a string from a user, and then:

  • Change every A character in the list to digit 1 ("1").
  • Change every B character in the list to digit 2 ("2").
  • Change every C character in the list to digit 3 ("3").

That's what I've done until now:

string = input("Enter a string: ")
for i in range(len(string) - 1):
    if string[i] == "A":
        string[i] = "1"
    elif string[i] == "B":
        string[i] = "2"
    elif string[i] == "C":
        string[i] = "3"

But when I run it, the interpreter gives me this error:

line 4, in string[i] = "1" TypeError: 'str' object does not support item assignment

Does someone know how to make my code work?

DaNiEl
  • 3
  • 3

2 Answers2

0

You can use that

user_row = input("Enter a string: ")
final_row = ''
for i in range(0, len(user_row)):
    if user_row[i] == "A":
        final_row += "1"
    elif user_row[i] == "B":
        final_row += "2"
    elif user_row[i] == "C":
        final_row += "3"
    else:
        final_row += user_row[i]
print(final_row)

Also

user_row = input("Enter a string: ")
user_row = user_row.replace('A', '1').replace('B', '2').replace('C', '3')
print(user_row)
t4kq
  • 754
  • 1
  • 5
  • 11
0

Strings are immutable.Insted of that do this:

user=input()
user=user.replace('A',1).replace('B',2).replace('C',3)

Here replace replaces all the occurrence of a with 1 and etc

EpicPy
  • 76
  • 1
  • 9