[Python] How do i convert a string into an integer?

Discussão em 'Python' iniciado por Stack, Abril 15, 2025.

  1. Stack

    Stack Membro Participativo

    So for my activity, I want to check if a car number is valid.
    To validate a given vehicle plate number with 4 digits starting with ‘S’, the following must be done:
    • Ignore the letter ‘S’
    • Assign a number to the letters as follows: A=1, B=2, C=3, …etc. For the number plate SBA 1234, this is converted to 21 1234
    • The 6 individual numbers need to be multiplied by 9,4,5,4,3,2 respectively and added together. For the example above, we will get, 2 x 9 + 1 x 4 + 1 x 5 + 2 x 4 + 3 x 3 + 4 x 2 = 52

    At the moment I managed to do the first two points and replace the letters with numbers, but for the part where I need to multiply the numbers respectively, I believe python still reads it(Newnum) as a string so I need to convert it to an integer so they can be multiplied.

    vehicle_num = input("Enter the vehicle number to be validated: ")
    if "S" in vehicle_num:
    Newnum = vehicle_num.replace("S","").replace("A","1").replace("B","2").replace("C","3")
    print(Newnum)
    else:
    print("Not working")
    multiplynum = Newnum

    multiplynum = ((Newnum[0] * 9) + (Newnum[1] * 4) + (Newnum[2] * 5) +
    (Newnum[3] * 4) + (Newnum[4] * 3) + (Newnum[5] * 2))
    print(Newnum)

    Continue reading...

Compartilhe esta Página