Sometimes, while working with Python, we can a problem in which we need to pad strings in lists at trailing or leading position. This kind of problem is quite common and can occur in day-day programming or web development. Let’s discuss a way in which this task can be performed.
Method : Using
+ operator + list comprehension
In this task, we just append the string at rear or front position using + operator and list comprehension is used to iterate through all the elements.
The original list : ['a', 'b', 'c', 'd'] list after prefix addition : ['gfga', 'gfgb', 'gfgc', 'gfgd'] list after suffix addition : ['agfg', 'bgfg', 'cgfg', 'dgfg']
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Perform append at beginning of list
- Python | Append String to list
- Python - Append Missing elements from other List
- Python - Append List every Nth index
- Python - Append given number with every element of the list
- Python | Remove empty strings from list of strings
- Python | Tokenizing strings in list of strings
- numpy.append() in Python
- append() and extend() in Python
- Python - Pandas dataframe.append()
- Python | Pandas Index.append()
- Python | Pandas TimedeltaIndex.append()
- Python | Append multiple lists at once
- Python | Pandas Series.append()
- Python | Append at front and remove from rear
- Python | Append Odd element twice
- Python append to a file
- Append to JSON file using Python
- Python | Append K character N times
- Python - Conditional String Append
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.