Change language

Python | Remove trailing / leading special characters from a list of strings

|

Method: Using map () + str.strip ()
Combining the above two functions can help us solve this particular problem. In this case, we use strip () , which has the ability to strip trailing and leading special unwanted characters from a list of strings.  map () , used to extend the logic for each item in the list.

# Python3 code to demonstrate how it works
# Remove trailing / leading special characters from the list of strings
# Using map () + str.strip ()

 
# initializing list

test_list = [ ’gfg’ , ’is’ , ’best’ ]

 
# print list

print ( " The original list: " + str (test_list))

 
# Remove trailing / leading special characters from the list of strings
# Using map () + str.strip ()

res = list ( map ( str . strip, test_list))

 
# Print result

print ( "List after removal of special characters:" + str (res))

Output:

 The original list: [’gfg’, ’is’, ’best’] List after removal of special characters: [’gfg’,’ is’, ’best’] 

Python | Remove trailing / leading special characters from a list of strings Python functions: Questions

Python | Remove trailing / leading special characters from a list of strings String Variables: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method