Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print the concatenation of the digits of two numbers in Python

Is there a way to concat numbers in Python, lets say I have the code

print(2, 1)

I want it to print 21, not 2 1and if i use "+", it prints 3. Is there a way to do this?

like image 205
Billjk Avatar asked Nov 26 '22 22:11

Billjk


2 Answers

Use the string formatting operator:

print "%d%d" % (2, 1)

EDIT: In Python 2.6+, you can also use the format() method of a string:

print("{0}{1}".format(2, 1))
like image 57
Rafael Almeida Avatar answered Nov 29 '22 12:11

Rafael Almeida


You could perhaps convert the integers to strings:

print(str(2)+str(1))
like image 40
Raul Marengo Avatar answered Nov 29 '22 14:11

Raul Marengo