string.whitespace in Python
Last Updated :
08 Jan, 2025
In Python, string.whitespace is a string containing all the characters that are considered whitespace. Whitespace characters include spaces, tabs, newlines and other characters that create space in text. string.whitespace constant is part of the string module, which provides a collection of string operations and constants.
Let's understand with an example:
Python
import string
s = "Hello\tWorld\n"
whitespace = [i for i in s if i in string.whitespace]
print(whitespace)
Explanation: In this example, the code identifies the tab (\t) and newline (\n) characters as whitespace in the given text.
Syntax of string.whitespace
The string.whitespace is simply a predefined constant in the string module. We can access it directly from the module as follows:
print(string.whitespace)
Parameters
- This constant does not accept any parameters as it is predefined in the
string
module.
Return Type
- Returns a string containing all whitespace characters recognized by Python.
Examples of string.whitespace in Python
1. Removing whitespace from a string
Whitespace characters often need to be removed from input strings for data processing.
Python
import string
s = "\tHello World!\n"
# Removing all whitespace characters
cleaned = ''.join(char for char in s if char not in string.whitespace)
print(cleaned)
Explanation
- generator expression filters out characters present in string.whitespace.
- join() method combines the remaining characters into a new string.
2. Validating strings for non-whitespace content
We can use string.whitespace to ensure a string contains meaningful content.
Python
import string
def is_non_empty(s):
return any(char not in string.whitespace for char in s)
print(is_non_empty("\t \n"))
print(is_non_empty("Python\n"))
Explanation
- any() function checks if any character in the string is not in string.whitespace.
- The result is False for strings that contain only whitespace and True otherwise.
3. Counting whitespace characters in a string
Counting whitespace characters can be done using the generator expression, let's see how:
Python
import string
s = "Python\tProgramming\nIs Fun"
# Counting whitespace characters
count = sum(1 for char in s if char in string.whitespace)
print(count)
Explanation
- generator expression iterates over the string and increments the count for each whitespace character.
- The total count of whitespace characters in the string is returned.
4. Splitting a string by whitespace characters
Let's see how we can split a string by whitespace characters.
Python
import string
s = "Hello\tPython\nWorld"
# Splitting the string using whitespace characters
split_text = ''.join(char if char not in string.whitespace else ' ' for char in s).split()
print(split_text)
Output['Hello', 'Python', 'World']
Explanation
- Whitespace characters are replaced with a single space.
- split() method then splits the string into a list of words based on spaces.
Similar Reads
Working with Strings in Python 3 In Python, sequences of characters are referred to as Strings. It used in Python to record text information, such as names. Python strings are "immutable" which means they cannot be changed after they are created.Creating a StringStrings can be created using single quotes, double quotes, or even tri
5 min read
Python String A string is a sequence of characters. Python treats anything inside quotes as a string. This includes letters, numbers, and symbols. Python has no character data type so single character is a string of length 1.Pythons = "GfG" print(s[1]) # access 2nd char s1 = s + s[0] # update print(s1) # printOut
6 min read
Python String strip() Method strip() method in Python removes all leading and trailing whitespace by default. You can also specify a set of characters to remove from both ends. It returns a new string and does not modify the original.Let's take an example to remove whitespace from both ends of a string.Pythons = " GeeksforGeeks
2 min read
Fill a Python String with Spaces This article will show you to fill out a python string with spaces to the left, right, and around a string in Python. Let's suppose we have a string GeeksforGeeks, and we want to fill N number of spaces on left, right, and around the string. Note that in the case of space around the string if N is o
3 min read
Triple Quotes in Python In Python, triple quotes let us write strings that span multiple lines, using either three single quotes (''') or three double quotes ("""). While theyâre most often used in docstrings (the text that explains how code works), they also have other features that can be used in a variety of situations
2 min read