How to Print a Sequence of Integers as a Continuous String in Python
👋 Welcome to my Hashnode blog! I'm a tech enthusiast and cloud advocate with expertise in IT, backup solutions, and cloud technologies. Currently, I’m diving deep into Python and exploring its applications in automation and data management. I share insights, tutorials, and tips on all things tech, aiming to help fellow developers and enthusiasts. Join me on this journey of learning and innovation!
In Python, working with sequences of numbers is a common task, especially when you need to print them in a specific format. One such task is to print a series of integers from 1 through n as a continuous string without spaces.
In this blog, I will guide you through a simple Python program that accomplishes this.
Problem Statement
Given a number n, you need to print the sequence of integers from 1 to n as a continuous string, without any spaces or separators.
For example, if n = 5, the expected output should be:
Copy code
12345
Solution Approach
We can break down the solution into the following steps:
Input the integer
nfrom the user.Generate a range of numbers from 1 through
n.Convert each number to a string.
Join all the strings together without spaces.
Print the result.
Let’s look at how this can be done in Python.
Code Example
Here’s the Python code to solve the problem:
pythonCopy code
n = int(input()) # Read input from the user
numbers = range(1, n+1) # Create a range from 1 to n
l = "".join(map(str, numbers)) # Convert each number to string and join without spaces
print(l) # Output the result
Explanation
Input Handling: The
input()function captures the input from the user, which is then converted into an integer usingint().pythonCopy code n = int(input()) # Read input from the userGenerate a Sequence of Numbers: We use
range(1, n+1)to create a sequence from 1 ton. Therange()function generates numbers from the first argument (inclusive) to the second argument (exclusive). Thus,range(1, n+1)includes the numbern.pythonCopy code numbers = range(1, n+1) # Create a range from 1 to nConvert and Join the Numbers: We need to convert each number in the sequence to a string. This is where the
map()function helps us by applyingstr()to each element innumbers. Next,"".join(...)concatenates all the strings without spaces, resulting in a continuous sequence.pythonCopy code l = "".join(map(str, numbers)) # Convert each number to string and join without spacesPrint the Result: Finally, we print the concatenated string.
pythonCopy code print(l) # Output the result
Example
Let’s walk through an example. Suppose the user inputs 5.
Input:
5Generated Sequence:
[1, 2, 3, 4, 5]String Representation:
'1', '2', '3', '4', '5'Final Output:
12345
Why This Approach?
Efficiency: The solution is optimized to work in linear time, as it processes each number in the sequence once.
Simplicity: Using
map()andjoin()together simplifies the process of converting numbers to strings and concatenating them without the need for additional loops.
Conclusion
Printing a sequence of numbers as a continuous string in Python can be done easily using the combination of map() and join(). This approach helps you keep your code clean and efficient while avoiding the use of loops for string concatenation.


