0

I am trying to loop over tasks list:

tasks = [
         'NR-AR', 'NR-AR-LBD', 'NR-AhR', 'NR-Aromatase', 'NR-ER', 'NR-ER-LBD',
         'NR-PPAR-gamma', 'SR-ARE', 'SR-ATAD5', 'SR-HSE', 'SR-MMP', 'SR-p53'
        ]

The output I get with:

for task in range(len(tasks)):
    print(tasks[task])

is:

NR-AR
NR-AR-LBD
NR-AhR
NR-Aromatase
NR-ER
NR-ER-LBD
NR-PPAR-gamma
SR-ARE
SR-ATAD5
SR-HSE
SR-MMP
SR-p53

What I was not able to do is getting the same output but with "..." for each task.

For example: "SR-p53" instead of SR-p53

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Triki Sadok
  • 120
  • 7
  • Does this answer your question? [How to print list elements in one line?](https://stackoverflow.com/questions/52097264/how-to-print-list-elements-in-one-line) – Tomerikoo Sep 17 '20 at 15:31
  • Correct dup: https://stackoverflow.com/questions/27757133/how-to-print-variable-inside-quotation-marks ; OR https://stackoverflow.com/questions/20056548/printing-double-quotes-around-a-variable – Tomerikoo Sep 17 '20 at 15:35
  • `for task in tasks: print(f'"{task}"')` – sjd Sep 17 '20 at 15:38
  • This is actually an X-Y problem. OP states that they want to assign the result to a variable to use it as string in one of the answer's comments. `tasks[task]` is already a string. – Asocia Sep 17 '20 at 15:48

2 Answers2

1

You can achieve this by either of the ways:

  1. Using format method of string

     print('"{}"'.format(tasks[task]))
    
  2. Formatting string with 'f':

     print(f'"{tasks[task]}"')
    
  3. Using modulus operator for formatting:

     print('"%s"' % tasks[task])
    
  4. Using string concatenation:

     print('"' + tasks[task] + '"')
    
Melvin Abraham
  • 2,870
  • 5
  • 19
  • 33
1

Use 'f' in print method to format and then include double quotes.

See the code below:

tasks = [
      'NR-AR', 'NR-AR-LBD', 'NR-AhR', 'NR-Aromatase', 'NR-ER', 'NR-ER-LBD',
      'NR-PPAR-gamma', 'SR-ARE', 'SR-ATAD5', 'SR-HSE', 'SR-MMP', 'SR-p53'
    ]

for task in range(len(tasks)):
    print(f'"{tasks[task]}"')
Kev Rob
  • 322
  • 2
  • 7
  • and can I use this to assign "SR-p53" as a string to a variable and not for print only ?? – Triki Sadok Sep 17 '20 at 15:43
  • 1
    @TrikiSadok Please see the duplicate I provided (https://stackoverflow.com/questions/20056548/printing-double-quotes-around-a-variable) It solves this question as well – Tomerikoo Sep 17 '20 at 15:44
  • 1
    @Asocia I believe he meant to save it with the quotes as a string variable which can be done by `s = '"SR-p53"'` – Tomerikoo Sep 17 '20 at 15:48
  • If you want to assign "SR-p53" you can put it in single quotes like this `string = ' "SR-p53" '`. If you print the variable string `print(string)` the output will be "SR-p53". – Kev Rob Sep 17 '20 at 15:51