import random
n=[random.randint(10,999) for i in range(int(input()))]
print(n,end=', ')
there is problem i need it to print whithouth squared brackets how i can change my code by not overcomplicating it
here is what it prints out
[385, 396, 37, 835, 376],
and it needs to look like this
305, 396, 37, 835, 376
I have tried puting brackets in multiple places also i have tried deleting things and all that hapens is it prints the same thing or there is error
CodePudding user response:
Convert the ints to strings and use join
:
>>> n = [385, 396, 37, 835, 376]
>>> print(", ".join(map(str, n)))
385, 396, 37, 835, 376
Another option is to use the *
operator to spread n
as multiple arguments to print
:
>>> print(*n, sep=", ")
385, 396, 37, 835, 376
CodePudding user response:
You have a list of integers. You can convert them to strings ans use the string.join
method to get the format you want.
import random
n=[random.randint(10,999) for i in range(int(input()))]
print(", ".join(str(val) for val in n),end=', ')
(Note: map
is an odd thing to use in a language that already has native mapping constructs. Best to keep it native).