Equivalent of R's paste command for vector of numbers in Python

>>> ["s" + str(i) for i in xrange(1,11)]
['s1', 's2', 's3', 's4', 's5', 's6', 's7', 's8', 's9', 's10']

EDIT: range works in both Python 2 and Python 3, but in Python 2 xrange is a little more efficient potentially (it's a generator not a list). Thansk @ytu


>>> list(map('s{}'.format, range(1, 11)))
['s1', 's2', 's3', 's4', 's5', 's6', 's7', 's8', 's9', 's10']

The answer by cdyson37 is the most pythonic one; of course you can still use range rather than xrange in your case.

In Python2, you can also put more emphasis on functional style with something like:

map(lambda x: "s"+str(x), range(1,11))

Tags:

Python

String

R