Insert the string at the beginning of all items in a list in Python


In this post we need to enter the string at the beginning of all items in a list. 

For ex: We're given string = "Tutorials_Point" and List contains multiple element such as "1", "2" etc. So in this we need to add Tutorials_Point in front of "1", "2" and so on.

Example

Aproach 1

 Live Demo

sample_list = [1, 2, 3]
print(['Tutorials_Point{0}'.format(i) for i in sample_list])

Output

//['Tutorials_Point1', 'Tutorials_Point2', 'Tutorials_Point3']

Approach 2

 Live Demo

sample_list = [1, 2, 3]
sample_str = 'Tutorials_Point'
sample_str += '{0}'
sample_list = ((map(sample_str.format, sample_list)))
print(sample_list)

Output

//['Tutorials_Point1', 'Tutorials_Point2', 'Tutorials_Point3']

Updated on: 16-May-2020

243 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements