
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Concatenate Two Integer Values into One in Python
An integer is a data type in Python that represents whole numbers without any fractional or decimal parts. In Python, integers are a built-in data type, and they can be used to perform arithmetic operations, store numerical values, and represent counts, indices, or other discrete quantities.
Integers in Python have a wide range of applications, including mathematical calculations, indexing and slicing sequences e.g., lists, strings, and controlling loops and iterations. They provide a fundamental building block for numerical computations and algorithm implementations in Python. The following are the examples of integers in python.
x = 5 y = -10 z = 0
In the above examples, x, y, and z are variables assigned with integer values. The value of x is 5, y is -10, and z is 0.
In this article we will go through different approaches in python for concatenating the two integers into one.
Using the str() function and string concatenation
In this approach, we convert both integers to strings using the str() function. Then, we use string concatenation + to concatenate the two strings together. Finally, we convert the resulting concatenated string back to an integer using the int() function.
Example
The below is the example for concatenate the two integers 123 and 456 into one.
def concatenate_integers(a, b): concatenated = str(a) + str(b) return int(concatenated) num1 = 123 num2 = 456 concatenated_num = concatenate_integers(num1, num2) print("The concatenate integers output:",concatenated_num)
Output
The concatenate integers output: 123456
Using String Formatting
In this approach, we use string formatting to concatenate the two integers into a single string. The {} placeholders in the format string are replaced with the values of a and b. Finally, we convert the concatenated string back to an integer.
Example
The following is the sample code for concatenate the two integers 678 and 890 into one.
def concatenate_integers(a, b): concatenated = "{}{}".format(a, b) return int(concatenated) num1 = 678 num2 = 890 concatenated_num = concatenate_integers(num1, num2) print("The concatenate integers output:",concatenated_num)
Output
The concatenate integers output: 678890
Using the Multiplication Operator
In this approach, we determine the multiplier by repeatedly multiplying it by 10 until it is greater than b. Then, we multiply a by the multiplier to shift its digits to the left, and add b to concatenate the two numbers together.
Example
def concatenate_integers(a, b): multiplier = 1 while multiplier <= b: multiplier *= 10 concatenated = a * multiplier + b return concatenated num1 = 123 num2 = 456 concatenated_num = concatenate_integers(num1, num2) print("The concatenate integers output:",concatenated_num)
Output
The concatenate integers output: 123456
Using the math.log10() function
In this approach, we calculate the number of digits in b using the logarithm base 10 math.log10() function. Then, we raise 10 to the power of the number of digits in b to get the multiplier. Finally, we multiply a by the multiplier and add b to concatenate the two numbers together.
Example
import math def concatenate_integers(a, b): num_digits_b = math.floor(math.log10(b)) + 1 multiplier = 10 ** num_digits_b concatenated = a * multiplier + b return concatenated num1 = 123 num2 = 456 concatenated_num = concatenate_integers(num1, num2) print("The concatenate integers output:",concatenated_num)
Output
The concatenate integers output: 123456