Python add suffix / add prefix to strings in a list

| | | | | | | | | | | | | | | | | | | |

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 suffix add prefix

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]

Python add_prefix
# 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

Python add_prefix

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically