remove_punctuation

pyhelpers.text.remove_punctuation(input_text, normalize_whitespace=True, preserve_kebab_case=True, preserve_snake_case=True, exclude=None)[source]

Removes punctuation from textual data.

Parameters:
  • input_text (str) – The input text.

  • normalize_whitespace (bool) – Whether to collapse all whitespace into single spaces. Defaults to True.

  • preserve_kebab_case (bool) – Whether to preserve hyphens in Kebab case (e.g., 'kebab-case'). Defaults to True.

  • preserve_snake_case – Whether to preserve underscores in Snake case (e.g. 'snake_case'). Defaults to True.

  • exclude (str | list | set | None) – Punctuation marks to always keep, overriding other parameters. Defaults to None.

Returns:

The processed text that is without punctuation (and optionally without whitespace).

Return type:

str

Examples:

>>> from pyhelpers.text import remove_punctuation
>>> remove_punctuation('Hello, world!')
'Hello world'
>>> input_text = '   How   are you? '
>>> remove_punctuation(input_text, normalize_whitespace=True)
'How are you'
>>> remove_punctuation(input_text, normalize_whitespace=False)
'How   are you'
>>> input_text = 'No-punctuation!'
>>> remove_punctuation(input_text, preserve_kebab_case=False)
'No punctuation'
>>> input_text = 'Hello world!  This is a test. :-)'
>>> remove_punctuation(input_text)
'Hello world This is a test'
>>> remove_punctuation(input_text, normalize_whitespace=False)
'Hello world    This is a test'
>>> input_text = "The 'hyphen' is-cool; but underscores_are_not."
>>> remove_punctuation(input_text)
'The hyphen is-cool but underscores_are_not'
>>> remove_punctuation(input_text, preserve_kebab_case=False, exclude=';')
'The hyphen is cool; but underscores_are_not'