Match Non-Whitespace Character in Python Using Regular Expression



A non-whitespace character is any character that is not a space, tab i.e., \t, newline i.e.,\n or carriage return \r. Examples of non-whitespace characters such as letters, digits, punctuation and special symbols.

In Python, Regular Expressions (RegEx) are used to match the patterns in the given input strings. The re module in Python provides different methods to use the regular expressions. This module helps the developers to perform search operations, validations, filtering and much more based on string patterns.

To match a non-whitespace character in Python we can use the special character class \S inside a raw string in any re module methods. In this article, let's see all the different method to match a non-whitespace character in Python using Regular Expressions.

Example: Finding First Non-Whitespace Character

In this example, we will use the method re.search() by passing \S inside a raw string to find the first non-whitespace character -

import re

text = "    \t  \nHello, Tutorialspoint!"
match = re.search(r"\S", text)

if match:
    print("First non-whitespace character:", match.group())
    print("Character position in string:", match.start())
else:
    print("No non-whitespace character found.")

Here is the output of the above example -

First non-whitespace character: H
Character position in string: 8

Example: Finding All Non-Whitespace Characters

In this example, we will use the method re.findall() by passing \S inside a raw string to find and return all non-whitespace characters from the input string -

import re

text = "  T u t o r i a l s \t p o i n t \n 2025 "
matches = re.findall(r"\S", text)

print("All non-whitespace characters:", matches)

Following is the output of the above example -

All non-whitespace characters: ['T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's', 'p', 'o', 'i', 'n', 't', '2', '0', '2', '5']

Whitespace vs Non-Whitespace Patterns

In Python regular expressions, different character classes are used to identify whitespace and non-whitespace characters. Below is a comparison of common patterns used to work with whitespace-related data -

Pattern Description Matches
\s Matches any whitespace character Space, tab, newline, carriage return
\S Matches any non-whitespace character Letters, digits, punctuation, symbols
\w Matches any word character Letters, digits, underscore (_)
\W Matches any non-word character Spaces, symbols, punctuation

These patterns are extremely useful when we want to validate, extract or clean text data based on the presence or absence of whitespace.

Updated on: 2025-06-09T09:55:51+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started