The normalize_whitespace()
method is a built-in function that is provided by the cleantext
library in Python. We can use it to perform the following operations:
from cleantext import clean
clean(text, normalize_whitespace=True)
The cleantext
package provides us with the clean
function.
text
: This is the text data to normalize.normalize_whitespace
: This is a Boolean value indicating whether to normalize whitespaces in the text. By default, the value is True
.The method returns the normalized text.
import cleantextstring = """hello educativehello edpresso """normalized_string = cleantext.clean(string, normalize_whitespace=True)print("Original String - '" + string + "'")print("\n")print("Normalized String - '" + normalized_string + "'")
cleantext
package.clean
method and passing normalize_whitespace
as True
.