Home > database >  Can't print without brackets
Can't print without brackets

Time:04-01

import random
n=[random.randint(10,999) for i in range(int(input()))]

print(n,end=', ')

The problem is I need it to print without squared brackets. How can I change my code by not over complicating 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 putting brackets in multiple places also I have tried deleting things and all that happens is it prints the same thing or there is error.

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).

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:

>>> n = [385, 396, 37, 835, 376]
>>> print(str(n)[1:-1])

385, 396, 37, 835, 376
  • Related