Space between result (Python)

Asked

Viewed 4,508 times

2

So, I’m doing an exercise where the goal is for the machine to ask a number and then it type the number according to its value. For example, if I type "2" it gives me the answer "22", if I type "3" it gives me the answer "333" and so on. I want between the numbers to be a space, like if I type "3" he gives me back "3 3 3". I know this seems to be very simple, but I’m a beginner I’m not finding a way to do it.

n1 = int(input("DIGITE UM NUMERO : "))
n2 = str(n1)
print(n3*n1)

3 answers

4

If you don’t mind leaving a blank at the end of the string displayed, just you concatenate (or interpolate) a blank space after the digit that was informed by the user:

n1 = input('Digite um número: ')
n2 = int(n1)

print(f'{n1} ' * n2)

When informing the value 2, the output would be '2 2 '. If you want, you can remove the white space from the ends with the method strip:

print((f'{n1} ' * n2).strip())

And so the exit becomes '2 2', without the space at the end.

Important considerations

  1. There is no need to turn the user input to integer and then again to string. See what way I did I kept the original entry and just converted once for whole;

  2. Instead of concatenating the white space, I used the interpolation of string through the f-strings;

Otherwise, another way to get the same result is by generating a string with the amount of elements you want and generate the string end from it, through the method join, that concatenates all values in the list using a string as a separator:

print(' '.join(n1 * n2))

The part n1 * n2 will generate a string 'n1n1n1...n1', with no spaces, with n2 elements and the method join will concatenate all values using the string ' ' as separator, thus the result would also be '2 2'.

  • 2

    You are the Pythonist the SOPT deserves.

  • @Or would it be pythonian? haha

3

I’m not Pythonist, but I thought of something like this: keep the input string intact, just make sure it’s really a number. Then multiply the concatenated input string with a space by converting to a number of its own.

entrada = input("DIGITE UM NUMERO : ")

if(entrada.isdigit()): 
    print((entrada + ' ') * int(entrada)) 
else:
    print(entrada, ' não é um número')

See working on Repl.it

2

In the second row the variable int(n) is converted to whole before being multiplied by n which is a string. Otherwise it would return an error.

The method join concatenates a space between characters of the variable n before being printed on the screen by print.

n = input("DIGITE UM NUMERO : ")
n = n * int(n)
print(" ".join(n))
  • 2

    Like the input already returns a string, I would keep her return from conversion to int and would do so only in multiplication: n * int(n). This would avoid converting the value of n for int and then to str again with no need.

  • True, it makes more sense. I didn’t realize when I copied the question code. Thank you for your contribution!

  • 1

    I corrected my answer as suggested by Anderson Carlos Woss.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.