What is the replace_urls() method in Python?

Overview

The replace_urls() method in Python replaces all the URLs in a given text with the replacement string.

What is clean-text?

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

The package can be installed using the pip command. We can check the following command to install the clean-text package.

pip install clean-text

Syntax

replace_urls(text, replace_with="<URL>")

Parameters

  • text: This is the text data.
  • replace_with: This is the replacement string.

Return value

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

Example

import cleantext
string = """hello educative - https://educative.io - hello edpresso"""
new_string = cleantext.replace_urls(string, replace_with="EDU_URL")
print("Original String - '" + string + "'")
print("Modified String - '" + new_string + "'")

Explanation

  • Line 1: We import the cleantext package.
  • Line 3: We define a string with an URL.
  • Line 5: We replace the URL in the string with EDU_URL using the replace_urls() method.
  • Lines 7–8: We print the original and modified string.

Free Resources