Skip to main content

Command Palette

Search for a command to run...

How to Print a Sequence of Integers as a Continuous String in Python

Published
3 min read
A

👋 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:

  1. Input the integer n from the user.

  2. Generate a range of numbers from 1 through n.

  3. Convert each number to a string.

  4. Join all the strings together without spaces.

  5. 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

  1. Input Handling: The input() function captures the input from the user, which is then converted into an integer using int().

     pythonCopy code
     n = int(input())  # Read input from the user
    
  2. Generate a Sequence of Numbers: We use range(1, n+1) to create a sequence from 1 to n. The range() function generates numbers from the first argument (inclusive) to the second argument (exclusive). Thus, range(1, n+1) includes the number n.

     pythonCopy code
     numbers = range(1, n+1)  # Create a range from 1 to n
    
  3. Convert 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 applying str() to each element in numbers. 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 spaces
    
  4. Print 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: 5

  • Generated 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() and join() 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.

More from this blog

PythonChallenge

11 posts

Welcome to my Python blog, where I share my journey of learning and exploring the versatile world of Python programming! 🌟

Let’s code, learn, and grow together in the world of Python programming!