Search code examples
pythonlistreplacesingle-quotes

generate a list of string elements without the quotation mark


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(''', '')

Solution

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