HackerRank Solution: Python Print Function [4 Methods]


Written by - Bashir Alam
Reviewed by - Deepak Prasad

Question: Python Print Function [Introduction]

The included code stub will read an integer, n, from STDIN.

Without using any string methods, try to print the following:

123...n

Note that "..." represents the consecutive values in between.

Example:

n  = 5

print string 12345

Input Format:

The first line contains an integer n.

Constraints:

1<=n<=150

Output Format

Print the list of integers from 1 through n as a string, without spaces

Sample input:

3

Sample output:

123

 

Possible solutions

This problem is very simple as we just have to print the string based on the input value. The following code is already given in the editor of the hacker rank:

if __name__ == '__main__':
    n = int(input())

Now, let us solve the problem using the following methods:

 

Solution-1: Using for loop

Now let us use the for loop to print the string in range of the input value:

if __name__ == '__main__':
    n = int(input())
    for i in range(1, n+1):
        print(i, end='')

This code snippet is checking to see if the current script being run is the main program (as opposed to being imported as a module into another script). If it is the main program, it then takes input from the user in the form of an integer, assigns it to the variable "n", and then uses a for loop to iterate over a range of numbers from 1 to n+1. Within the loop, it then prints the current iteration number, with the "end" parameter set to an empty string, which means that each number will be printed on the same line, rather than on a new line.

 

Solution-2: Using list comprehension

Now let us shorten the code and print the list using one line of code:

if __name__ == '__main__':
    n = int(input())
    print(*[i for i in range(1,n+1)],sep="")

This code snippet is similar to the previous one, but it uses a more concise approach to achieve the same result. It's checking to see if the current script being run is the main program and then takes input from the user in the form of an integer and assigns it to the variable "n". Instead of using a for loop, it's using a list comprehension to generate a list of numbers from 1 to n+1 and then using the "*" operator to unpack the list and print all the elements as individual arguments to the print function. And it is using the sep parameter to set the separator between the numbers as an empty string. This way it will print all the numbers in a single line, just like the previous code snippet.

 

Solution-3: Using map() function

We can also solve the problem using map() function:

if __name__ == '__main__':
    n = int(input())
    print("".join(map(str,range(1,n+1))))

This code snippet is similar to the previous one, it's checking to see if the current script being run is the main program and then takes input from the user in the form of an integer and assigns it to the variable "n". Instead of using a list comprehension, it's using the range() function to generate a sequence of numbers from 1 to n+1 and passing it to the map() function which applies the str() function to each element of the sequence. The str() function converts each element of the sequence to a string. Then it uses the join() function to concatenate all the elements of the sequence into a single string. Finally, it's printing the result of the join() function. This way it will print all the numbers in a single line, just like the previous code snippets.

 

Solution-4: Using a while loop

Now let us use the while loop to solve the problem:

if __name__ == '__main__':
    n = int(input())
    i = 1
    while(i <= n):
        print(i,end='')
        i+=1

This code snippet is similar to the previous one, it's checking to see if the current script being run is the main program and then takes input from the user in the form of an integer and assigns it to the variable "n". Instead of using a for loop, it's using a while loop which will iterate until a certain condition is met. In this case, it's using a while loop to iterate while the value of the variable i is less than or equal to n. Within the loop, it then prints the current value of i, with the "end" parameter set to an empty string, which means that each number will be printed on the same line, rather than on a new line. Then it's incrementing the value of i by 1 after each iteration so that the loop can terminate when i becomes greater than n. This way it will print all the numbers in a single line, just like the previous code snippets.

 

Summary

In this short article, we discussed how to solve the print problem from hacker rank. We discussed four different methods to solve the problem.

 

Further reading

Question on Hacker Rank: Python Print Function [Introduction]

 

Views: 296

Bashir Alam

He is a Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in OCR, text extraction, data preprocessing, and predictive models. You can reach out to him on his Linkedin or check his projects on GitHub page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment