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
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;
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'
.
You are the Pythonist the SOPT deserves.
– Jéf Bueno
@Or would it be pythonian? haha
– Woss