Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python

What is replace_digits() method of clean-text package in python?

Abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

What is the clean-text package?

clean-text is a third-party package that is used to pre-process text data to obtain a normalized text representation.

The package can be installed via pip. Check the following command to install the clean-text package.

pip install clean-text

replace_digits() method

The replace_digits() method replaces all the digits in the given text with the replacement string.

Method signature

replace_digits(text, replace_with="0")

Parameters

  • text: The text data.
  • replace_with: The replacement string.

Return value

The method returns the text data where all the digits are replaced by the replacement string.

Code

import cleantext
string = """hello educative - 2-3-4 - hello edpresso"""
new_string = cleantext.replace_digits(string, replace_with="?")
print("Original String - '" + string + "'")
print("Modified String - '" + new_string + "'")
Demonstration of the `replace_digits()` method to replace digits with strings

Code explanation

  • Line 1: We import the cleantext package.
  • Line 3: We define a string with digits in it.
  • Line 5: Every digit in the string is replaced with ? using the replace_urls() method.
  • Lines 7-8: We print the original and the modified strings.

RELATED TAGS

python

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring