string.whitespace in Python
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:
import string
s = "Hello\tWorld\n"
whitespace = [i for i in s if i in string.whitespace]
print(whitespace)
Output
['\t', '\n']
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.
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)
Output
HelloWorld!
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.
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"))
Output
False True
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:
import string
s = "Python\tProgramming\nIs Fun"
# Counting whitespace characters
count = sum(1 for char in s if char in string.whitespace)
print(count)
Output
3
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.
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.