Concatenate 2 numbers in python
Example 1: (Example 2 is much faster, don't say I didn't warn you!) Show
Example:
Example 2: For people who think
Example:
EDIT: I thought it would be convienient to time these codes, look below:
The times:
Conclusion:
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 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.
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.
Output: 90691 View Discussion Improve Article Save Article View Discussion Improve Article Save Article Given two integers n1 and n2, the task is to concatenate these two integers into one integer. Input: n1 = 12, n2 = 34 Output: 1234 Input: n1 = 1, n2 = 93 Output: 193 Approach: The simplest approach to do this is:
Program: C++
C
Java
Python3
C#
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.
|