How to concatenate two integers in Python?

前端 未结 14 1959
南旧
南旧 2020-12-15 03:51

How do I concatenate two integer numbers (for example: 10 and 20) in Python to get a returned value of 1020?

14条回答
  •  长情又很酷
    2020-12-15 04:19

    Using Math converter is faster than converting to string and back in my testing:

    In [28]: fn = lambda x, y: x*10 + y                                                                               
    
    In [29]: timeit fn(1,2)                                                                                            
    88.4 ns ± 1.26 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
    
    In [30]: timeit int(str(1) + str(2))                                                                               
    427 ns ± 11.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
    

提交回复
热议问题