Method: Using the + operator
+ list comprehension
In this task, we simply add a string to the back or front position using the + operator, and list comprehension is used to iterate over all elements.
# Python3 code to demonstrate working of # Append suffix / prefix to strings in list # Using list comprehension + "+" operator # initializing list test_list = [’a’, ’b’, ’c’, ’d’] # printing list print("The original list : " + str(test_list)) # initializing append_str append_str = ’gfg’ # Append suffix / prefix to strings in list pre_res = [append_str + sub for sub in test_list] suf_res = [sub + append_str for sub in test_list] # Printing result print("list after prefix addition : " + str(pre_res)) print("list after suffix addition : " + str(suf_res))
Output:
The original list: [’a’, ’b’,’ c’, ’d’] list after prefix addition: [’ gfga’, ’gfgb’,’ gfgc’, ’gfgd’] list afterix addition: [’ agfg’, ’bgfg’,’ cgfg ’,’ dgfg’]

Python add_prefix
Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one such package and makes it much easier to import and analyze data.
Dataframe.add_prefix() can be used with both series and dataframes.
- For series, row labels are prefixed.
- For a DataFrame, the column labels are prefixed.
Syntax: DataFrame.add_prefix(prefix)
Parameters:
prefix : string
Returns: with_prefix: type of caller
Example #1: Prefix col_
in each columns in the dataframe
# importing pandas as pd import pandas as pd # Making data frame from the csv file df = pd.read_csv("nba.csv") # Printing the first 10 rows of the # dataframe for visualization df[:10]

# Using add_prefix() function # to add ’col_’ in each column label df = df.add_prefix(’col_’) # Print the dataframe df
Example #2: Using add_prefix()
with Series in pandas
add_prefix()
alters the row index labels in the case of series.
# importing pandas as pd import pandas as pd # Creating a Series df = pd.Series([1, 2, 3, 4, 5, 10, 11, 21, 4]) # This will prefix ’Row_’ in # each row of the series df = df.add_prefix(’Row_’) # Print the Series df
