0
Nums = ['1', '2', '3', '4']

print(str('\n'.join(Nums)) + 'XXX')

Currently this returns

1
2
3
4XXX

I'd like to get the code to return xxx after each number rather than after only the last! Is there a way to process each one of these individually so it prints each entry in Nums + XXX?

Example:

1XXX

2XXX
...

Thanks!

Dann
  • 159
  • 6

2 Answers2

6
In [1]: Nums = ['1', '2', '3', '4']

In [2]: print('\n'.join([i+'XXX' for i in Nums]))
1XXX
2XXX
3XXX
4XXX

To print each line separately:

In [5]: for i in Nums:
   ...:     print('{}XXX\n'.format(i))
   ...:
1XXX

2XXX

3XXX

4XXX
Osman Mamun
  • 2,864
  • 1
  • 16
  • 22
  • While this is definitely close to what I'm asking for, is there a way to print each line/`#XXX` individually? This way you can add each result to a list like `Nums` – Dann Feb 06 '19 at 04:49
  • sure, I'll add a modification shortly. – Osman Mamun Feb 06 '19 at 04:50
  • Works flawlessly! Do you know where I can find the documentation for your solution? I'm fairly new to Python and would like to learn as much as I can before coming here for help – Dann Feb 06 '19 at 04:56
  • Glad it worked! You can follow this `https://www.programiz.com/python-programming/methods/string/join` or some other good tutorials on string join method. – Osman Mamun Feb 06 '19 at 04:58
2

It's similar to what you did with a small modification.

Nums = ['1', '2', '3', '4']

print('XXX\n'.join(Nums) + 'XXX\n')

I hope this helps!.

bumblebee
  • 1,811
  • 12
  • 19