Python: Cannot understand why the error - cannot concatenate 'str' and 'int' object ?

PythonServer Side ProgrammingProgramming

This can be corrected by putting n+1 in brackets i.e. (n+1)

for num in range(5):
    print ("%d" % (num+1))

Using %d casts the object following % to string. Since a string object can't be concatnated with a number (1 in this case) interpreter displays typeerror.

raja
Published on 24-Apr-2018 19:38:19
Advertisements