Making a string out of a string and an integer in Python

Python 3.6 has f-strings where you can directly put the variable names without the need to use format:

>>> num=12
>>> f"b{num}"
'b12'

name = 'b' + str(num)

or

name = 'b%s' % num

Note that the second approach is deprecated in 3.x.


Python won't automatically convert types in the way that languages such as JavaScript or PHP do.

You have to convert it to a string, or use a formatting method.

name="b"+str(num)

or printf style formatting (this has been deprecated in python3)

name="b%s" % (num,)

or the new .format string method

name="b{0}".format(num)