Search code examples
pythonpython-3.x

Create "n" digit number using just 9s where "n" is variable


For example, if user inputs 5, output: 99999

I know I can use a for loop to string concatenate, and then int the string, but this solutions seems ineffective.


Solution

  • You can simply do this:

    >>> my_count = 5
    >>> int('9'*my_count)
    99999
    

    Here, '9'*my_count will repeat '9' five (my_count) times in a string and then I am type-casting it to int to get an integer value.

    Note: If you want return value as integer, then go with kaya3's answer as it'll give better performance. But if you want the return value as string (without type-casting it to int), only then you should go with this answer as it will be more performance efficient.