1

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

1
  • Python 2.7 or Python3? Commented Dec 10, 2016 at 16:14

6 Answers 6

11

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='')
Sign up to request clarification or add additional context in comments.

9 Comments

Supported on Python3
This answer is not supported in python 2. If anyone is getting error, check python version.
@ShivamSingh Fair point. But to be honest we should stop talking about python 2 all at once because it'll die quite soon.
@yevhenKuzmovych yea but i too came across the same question and after searching for the answer here, i found our that i wasn't working because python 2 compiler was selected. So i wrote this comment for people who doing it on online compilers where pyhton 2 is still an option.
One thing that bears explaining: the Hackerrank challenge in question specifies not using any string functions. Although this answer is easily found in a simple web search, it can look like a wrong answer because a lot of the articles refer to the empty quotation marks as an "empty string," which could look like it violates the no string functions requirement.
|
3

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

2 Comments

ok. so what is the method to work around for number more than 10
Updated the answer
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 = "")
    

Comments

1
n=int(input())

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

2 Comments

Please augment your code-only answer with some explanation, in order to help fighting the misconception that StackOverflow is a free coding service.
@AyushBasera Your proposed edit would make this code-only answer functionally identical to your code-only answer, which requires explanation. Also, editing other authors code with functional changes is not appreciated.
1

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

Comments

1

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)

2 Comments

Welcome to Stack Overflow. Code dumps without any explanation are rarely helpful. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please edit your question and explain how it works better than what the OP provided. See How to Answer.
Please try to give some explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.