[Python] How to convert string to int in Python?

Discussão em 'Python' iniciado por Stack, Maio 27, 2025.

  1. Stack

    Stack Membro Participativo

    I am reading a number from a text file and trying to convert it into an int but it keeps giving me errors. This is my code:

    f = open('commentcount.txt', 'r')
    counts = f.readline()
    int(counts)
    counts = counts + 1
    print(counts)


    I am getting this error:

    counts = counts + 1
    TypeError: Can't convert 'int' object to str implicitly


    What am I doing wrong?

    Continue reading...

Compartilhe esta Página