delete all whitespace python code example

Example 1: replacing spaces in a string python

mystring.replace(" ", "_")

Example 2: remove all whitespace from string python

import re
s = '\n \t this is a string   with a lot of whitespace\t'
s = re.sub('\s+', '', s)

Example 3: python remove similar string with whitespace from list

def strip_list_noempty(mylist):
    newlist = (item.strip() if hasattr(item, 'strip') else item for item in mylist)
    return [item for item in newlist if item or not hasattr(item, 'strip')]