Concatenate 2 numbers in python

Example 1: (Example 2 is much faster, don't say I didn't warn you!)

a = 9
b = 8
def concat(a, b):
    return eval(f"{a}{b}")

Example:

>>> concat(a, b)
98

Example 2:

For people who think eval is 'evil', here's another way to do it:

a = 6
b = 7
def concat(a, b):
    return int(f"{a}{b}")

Example:

>>> concat(a, b)
67

EDIT:

I thought it would be convienient to time these codes, look below:

>>> min(timeit.repeat("for x in range(100): int(str(a) + str(b))", "",
          number=100000, globals = {'a': 10, 'b': 20}))
9.107237317533617
>>> min(timeit.repeat("for x in range(100): int(f'{a}{b}')", "",
          number=100000, globals = {'a': 10, 'b': 20}))
6.4986298607643675
>>> min(timeit.repeat("for x in range(5): eval(f'{a}{b}')", "", #notice the range(5) instead of the range(100)
          number=100000, globals = {'a': 10, 'b': 20}))
4.089137231865948 #x20

The times:

eval: about 1 minute and 21 seconds.

original answer: about 9 seconds.

my answer: about 6 and a half seconds.

Conclusion:

The original answer does look more readable, but if you need a good speed, choose int(f'{vara}{varb}')

P.S: My int(f'{a}{b}) syntax only works on python 3.6+, as the f'' syntax is undefined at python versions 3.6-

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given two integers a and b. The task is to concatenate these two integers into one integer.

    Examples:

    Input : a = 806, b = 91
    Output : 80691
    
    Input : a = 5, b = 1091
    Output : 51091
    
    

    Method 1: One method of achieving this can be counting the number of digits of second number. Then multiply the first number with 10^digits and adding both the numbers. Below is the implementation.

    def numConcat(num1, num2):

         digits = len(str(num2))

         num1 = num1 * (10**digits)

         num1 += num2

         return num1

    a = 906

    b = 91

    print(numConcat(a, b))

    Method 2: Another method can be converting both the numbers to the string. Then concatenate them and convert them back to integers. Below is the implementation.

    def numConcat(num1, num2):

            num1 = str(num1)

            num2 = str(num2)

            num1 += num2

            return int(num1)

    a = 906

    b = 91

    print(numConcat(a, b))

    Output:

    90691
    

    View Discussion

    Improve Article

    Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given two integers n1 and n2, the task is to concatenate these two integers into one integer.
    Example: 
     

    Input: n1 = 12, n2 = 34
    Output: 1234
    
    Input: n1 = 1, n2 = 93
    Output: 193

    Approach: The simplest approach to do this is: 
     

    • Convert both numbers to string
    • Concatenate both strings into one, as this is comparatively easy
    • Convert this concatenated string back to integer

    Program:
     

    C++

    #include

    #include

    using namespace std;

    int concat(int a, int b)

    {

        string s1 = to_string(a);

        string s2 = to_string(b);

        string s = s1 + s2;

        int c = stoi(s);

        return c;

    }

    int main()

    {

        int a = 23;

        int b = 43;

        cout << concat(a, b) << endl;

        return 0;

    }

    C

    #include

    #include

    #include

    int concat(int a, int b)

    {

        char s1[20];

        char s2[20];

        sprintf(s1, "%d", a);

        sprintf(s2, "%d", b);

        strcat(s1, s2);

        int c = atoi(s1);

        return c;

    }

    int main()

    {

        int a = 23;

        int b = 43;

        printf("%d\n", concat(a, b));

        return 0;

    }

    Java

    public class GFG {

        static int concat(int a, int b)

        {

            String s1 = Integer.toString(a);

            String s2 = Integer.toString(b);

            String s = s1 + s2;

            int c = Integer.parseInt(s);

            return c;

        }

        public static void main(String args[])

        {

            int a = 23;

            int b = 43;

            System.out.println(concat(a, b));

        }

    }

    Python3

    def concat(a, b):

        s1 = str(a)

        s2 = str(b)

        s = s1 + s2

        c = int(s)

        return c

    a = 23

    b = 43

    print(concat(a, b))

    C#

    using System;

    class GFG

    {

        static int concat(int a, int b)

        {

            String s1 = a.ToString();

            String s2 = b.ToString();

            String s = s1 + s2;

            int c = int.Parse(s);

            return c;

        }

        public static void Main(String []args)

        {

            int a = 23;

            int b = 43;

            Console.WriteLine(concat(a, b));

        }

    }

    Javascript

    Output: 

    2343

    Time Complexity: O(d1+d2) where d1=log10(a) and d2=log10(b)

    Auxiliary Space: O(d1+d2)
     


    How do you concatenate numbers?

    This means that you can no longer perform any math operations on them. To combine numbers, use the CONCATENATE or CONCAT, TEXT or TEXTJOIN functions, and the ampersand (&) operator. Notes: In Excel 2016, Excel Mobile, and Excel for the web, CONCATENATE has been replaced with the CONCAT function.

    How do you concatenate strings and integers in Python?

    Python Concatenate String and int.
    Using str() function. The easiest way is to convert int to a string using str() function. ... .
    Using % Operator. print("%s%s" % (s, y)).
    Using format() function. We can use string format() function too for concatenation of string and int. ... .
    Using f-strings..

    How do you concatenate a list of elements in Python?

    To concatenate a list of integers an empty list is created as newlist = []. The extend method() adds all the elements of the list till the end. To concatenate the list on integers “+” is used. The print(newlist) is used to get the output.