Hackerrank - Print Function Solution
1 min read

Hackerrank - Print Function Solution

Hackerrank - Print Function Solution
Hackerrank - Print Function Solution

Check Tutorial tab to know how to to solve.

Read an integer .

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

Note that "" represents the values in between.

Input Format

The first line contains an integer .

Output Format

Output the answer as explained in the task.

Sample Input 0

3

Sample Output 0

123

Solution in python

Solution 1.

n = int(input())
for i in range(1,n+1):
    print(i,end="")


Solution 2.

n = int(input())
print(*range(1,n+1),sep="")

Explanation

Range gives us numbers between 2 given numbers

>>> range(0,4)
[0,1,2,3]
>>> range(2,5)
[2,3,4]
>>> range(3,7)
[3,4,5,6]

The * is used to unpack a iterable(list, string,tuple etc) in python

>>> print(*range(0,4))
0 1 2 3
>>> print(*(2,3))
2 3
>>> print(*[0,5,7])
0 5 7

By default the output is separated by a space " "

To remove the space we can use  sep=""

>>> print(*range(0,4), sep="")
0123
>>> print(*(2,3),sep=",")
2,3
>>> print(*[0,5,7], sep="\n")
0
5
7

Enjoying these posts? Subscribe for more


Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.

×