How to strip spaces/tabs/newlines using Python regular expression?


The following code strips the spaces/tabs/newlines from given string using Python regex

We use '\S' in regex which stands for all non-whitespace characters

Example

import re
print re.findall(r"[\S]","""I find
    Tutorialspoint useful""")

Output

This gives the output

['I', 'f', 'i', 'n', 'd', 'T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's', 'p', 'o', 'i', 'n', 't', 'u', 's', 'e', 'f', 'u', 'l']

Updated on: 19-Feb-2020

358 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements