-1

So I'm making a basic table generator that will take a number from the user and print the table using for loop using the range function:

n = int(input('Enter number for table'))
for i in range(1,11):
    # print(n*i, '\n')
    print(n + "" + i + "=" + n*i)

I want the table to be printed in the following format:

e.g: 5*4=20

I'm writing print(n + "*" + i + "=" + n*i).

Here n is the user given number for which table is required and i is from for loop but the program returns an error saying can not concatenate str and int.

I'm new to python I'm coming from C++ it was easy in C++ to write these sorts of statements but I don't know how to do it in python.

Thank you

enter image description here

Cubix48
  • 2,607
  • 2
  • 5
  • 17

1 Answers1

0

You cannot concatenate strings with integers or floats. Either converting the int/float to string or using a , instead of + would work.

  1. print(n , "*" , i , "=" , n*i) # Also adds a space between elements. Or
  2. print(str(n) + "*" + str(i) + "=" + str(n*i))
Anshumaan Mishra
  • 1,349
  • 1
  • 4
  • 19
  • Or use a formatted string literal like we do in 2022 and be done with it :-) `print(f"{n} * {i} = {n*i})"` – ypnos Apr 02 '22 at 11:04