Some Dude
Some Dude

Reputation: 207

Adding text to loop in python

I am curios how to do this in Python, Powershell no prob. Python will be the death of me. Say I have a loop that prints a range, such as

print"\n".join(['{0:04}'.format(num) for num in xrange(0, 10000)])

This of course will return a list of

0000 0001 0002 0003 ..... 9999

I am curious how to pipe this, or set the string as a variable so I could get reoccurring text before each joined string. Example, Test0000 Test0001 Test0002 .... Test9999. You get the picture. I have set it as a variable and experimented with that, but I cannot seem to get how to include the Test in the joined string. My results so far have ended up with Test0000 0001 0002 blah blah blah.

Help would be appreciated.

Upvotes: 2

Views: 1192

Answers (2)

heemayl
heemayl

Reputation: 42017

You can use map too:

Python 3:

print('\n'.join(map(lambda x: 'Test{0:04}'.format(x), range(0, 10000))))

Python 2:

print '\n'.join(map(lambda x: 'Test{0:04}'.format(x), xrange(0, 10000)))

Upvotes: 1

smac89
smac89

Reputation: 43098

print"\n".join(['Test{0:04}'.format(num) for num in xrange(0, 10000)])

Upvotes: 4

Related Questions