Input 5 output 12345 in python

I am new to python and I was practicing my coding skills with Hacker rank. I came across a question where I have to print numbers in a series one after another without adding any new line or white space in between them and most important is I cannot use the string method to get desired output.

For example if input is

3

then output should be

123

How can do it without string?

Please guide me...

Thanks in advance

asked Dec 10, 2016 at 16:12

1

You can use end keyword in print[] method to avoid spaces:

num = int[input["Enter number: "]]
for i in range[1, num+1]:
    print[i, end='']

answered Dec 10, 2016 at 16:21

Yevhen KuzmovychYevhen Kuzmovych

8,0355 gold badges24 silver badges41 bronze badges

4

You may create a custom function using range[] along with the usage of math.log10[] as:

import math

def get_my_number[num]:
    my_num = 0
    for i in range[1, num+1]:
        digits = int[math.log10[i]]+1  # count number of digits in number
        my_num = my_num * [10 ** digits] + i
    return my_num

Sample run:

>>> get_my_number[3]
123
>>> get_my_number[13]  # For number greater than 10
12345678910111213

This way you will get the actual value and you won't be just printing it on the console

answered Dec 10, 2016 at 16:17

Moinuddin QuadriMoinuddin Quadri

44.6k12 gold badges92 silver badges118 bronze badges

2

for statement below takes elements starting from 1 to n.

  • If you use "in range[n]" it would print the elements starting from 0 to n-1.
  • If you use "in range[1,n]" it would print the elements starting from 1 to n-1.
  • If you use "in range[1,n+1]" it would print the elements starting from 0 to n. And this is what you want.

printing without new line requires and addition in print command.

  • end = "" would print 123 for example.

  • end = " " with 1 space between would print 1 2 3 and so on.

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

answered Feb 15, 2021 at 11:09

n=int[input[]]

for i in range[n]:    
    print[i+1, end=""]

timgeb

74.5k20 gold badges114 silver badges139 bronze badges

answered Dec 8, 2018 at 13:12

2

The following copy integer values to array ls before writing to a buffer pr and making a single write call [print]:

n = int[input[]]
ls = []
pr = [""]
for i in range[1,n+1]:
    ls.append[i]
for i in range[len[ls]]:
    pr += "{}".format[ls[i]]
print[pr]

answered Mar 11, 2021 at 18:12

2

If you want to print n natural number without using string then you can try this code:

n=int[input[]]
print[*range[1,n+1],sep=""]

simple input: 5
output will be: 12345

Gino Mempin

20.8k24 gold badges84 silver badges111 bronze badges

answered Dec 2, 2019 at 21:46

0

How do I print 12345 in Python?

how to print 12345 in one row without using string.
+8. print[*[n for n in range[1,6]], sep=""] or just print[12345] as suggested by Diego. ... .
+6. Language - Python This might be cheating but try it: num = 12345; print[num] And next time please specify the programming language. ... .
+5. ... .
+3. ... .
+2. ... .
+2. ... .
+1. ... .

How do you print 5 numbers in Python?

To print 1 to 50, you need to pass n+1 for eg, 1 to 51 in range function, its [i=1;i

Chủ Đề