Home > database >  generate a list of string elements without the quotation mark
generate a list of string elements without the quotation mark

Time:01-27

I would like to generate a list of string elements, but without the quotation marks. This is how I am generating the list:

  [f'test_{i 1}' for i in range(5)]

This yields the following result:

['test_1', 'test_2', 'test_3', 'test_4', 'test_5']

How do I remove the quotaton marks? I tried as shown below but this gives me a syntax error.

   [f'test_{i 1}' for i in range(5)].replace(''', '')

CodePudding user response:

There are no quotation marks in your strings. The quotation marks you trying to remove are a part of the Python syntax. They are necessary to delimit your strings. You cannot remove them.

P.S: Python lists have no replace method. If you want to replace anything within the string, the following syntax will do:

a = # the character to be replaced
b = # the character to replace a
[f'test_{i 1}'.replace(a, b) for i in range(5)]

CodePudding user response:

If for some reason you cannot have quotes in the print statement, you can use

print(', '.join(['test_1', 'test_2', 'test_3', 'test_4', 'test_5']))

Note that this is joining all of the elements together into a single string.

  •  Tags:  
  • Related