Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to concatenate two Integer values into one
An integer is a data type in Python that represents whole numbers without any fractional or decimal parts. Integers are built-in data types used for arithmetic operations, storing numerical values, and representing counts or indices.
In this article, we will explore different approaches to concatenate two integers into one single integer value in Python.
Using str() Function and String Concatenation
This approach converts both integers to strings using str(), concatenates them with the + operator, then converts back to integer ?
def concatenate_integers(a, b):
concatenated = str(a) + str(b)
return int(concatenated)
num1 = 123
num2 = 456
result = concatenate_integers(num1, num2)
print("Concatenated result:", result)
Concatenated result: 123456
Using String Formatting
String formatting with .format() provides a cleaner approach to combine integers into a string before converting back ?
def concatenate_integers(a, b):
concatenated = "{}{}".format(a, b)
return int(concatenated)
num1 = 678
num2 = 890
result = concatenate_integers(num1, num2)
print("Concatenated result:", result)
Concatenated result: 678890
Using f-strings (Python 3.6+)
F-strings offer the most readable and efficient string formatting approach ?
def concatenate_integers(a, b):
return int(f"{a}{b}")
num1 = 321
num2 = 654
result = concatenate_integers(num1, num2)
print("Concatenated result:", result)
Concatenated result: 321654
Using Mathematical Approach
This method multiplies the first number by the appropriate power of 10 based on the digit count of the second number ?
def concatenate_integers(a, b):
# Count digits in b by converting to string
num_digits_b = len(str(b))
multiplier = 10 ** num_digits_b
return a * multiplier + b
num1 = 123
num2 = 456
result = concatenate_integers(num1, num2)
print("Concatenated result:", result)
Concatenated result: 123456
Using math.log10() for Digit Counting
This approach uses logarithms to calculate the number of digits mathematically ?
import math
def concatenate_integers(a, b):
if b == 0:
return a * 10 # Handle edge case for zero
num_digits_b = math.floor(math.log10(b)) + 1
multiplier = 10 ** num_digits_b
return a * multiplier + b
num1 = 123
num2 = 456
result = concatenate_integers(num1, num2)
print("Concatenated result:", result)
Concatenated result: 123456
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| str() + concatenation | High | Medium | Simple cases |
| f-strings | Highest | High | Modern Python (3.6+) |
| Mathematical approach | Medium | Highest | Performance-critical code |
| math.log10() | Low | Medium | Educational purposes |
Conclusion
For most use cases, f-strings provide the best balance of readability and performance. Use mathematical approaches when performance is critical and string conversion overhead should be avoided.
