Print Function Python – HackerRank Solution

Print Function Python

print(): This function is used standard output

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

Print the string 12345.

Input Format

The first line contains an integer .

Constraints

1<=n<=150

Output Format

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

Sample Input 0

3

Sample Output 0

123

Code:

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

This is a Python code that takes an integer input n from the user and prints all the numbers from 1 to n on a single line, with no space between them.

Here’s a step-by-step explanation of what’s happening in the code:

The first line checks if the module is being run as the main program or if it is being imported as a module into another program.

If the module is being run as the main program, the code inside the if block will be executed. If it is being imported, the code inside the if block will not be executed.

The second line takes an integer input from the user using the input() function and converts it to an integer using the int() function. The integer value is then stored in the variable n.

The for loop is then used to iterate over a range of values from 1 to n (inclusive). The range() function generates a sequence of numbers starting from 1 up to n, with a step of 1.

Inside the loop, the print() function is used to print the value of the loop variable i. The end parameter is used to specify what character should be printed after the value of i.

By default, the end parameter is set to ‘\n’, which prints a newline character after each value.

In this case, the end parameter is set to an empty string “”, which means that nothing will be printed after each value of i.

When the loop is finished, all the numbers from 1 to n will have been printed on a single line, with no space between them.

Disclaimer: These problems are originally created and published by HackerRank, we only provide solutions to those problems. Hence, doesn’t guarantee the truthfulness of the problem. This is only for information purposes.

Leave a Comment