1
votes

I have some text in an input file. The text is cleaned by removing white spaces occurring in it. Sample text looks as follows:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras mattis purus nec aliquam placerat. Donec efficitur ex vel ante mattis fermentum. Fusce consequat placerat lectus a volutpat. Nulla vitae feugiat ex. Ut at sollicitudin felis. Curabitur efficitur ligula molestie lorem sagittis, eu blandit mi sagittis. Duis scelerisque blandit porta. In vel nunc quam. Phasellus aliquet nunc et nibh ullamcorper, at ullamcorper odio cursus. Suspendisse gravida erat ac urna luctus, nec fermentum nulla tincidunt. Etiam sollicitudin bibendum tristique. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Maecenas auctor nulla eu faucibus maximus.

What built-in string methods of the Python Standard Library can be used instead of writing a new method to strip the white space characters in the above sample text?

I'm using Python 3.6

3
Do you want to remove all white spaces? - Epsi95
What have you tried so far? - StarShine
@Epsi95, yes, I would want to remove each white space contained in the sample text. - Pooja
@StarShine, I've tried processing the sample text with lstrip, rstrip, strip methods. - Pooja
@Pooja you mean "I am Pooja" to "IamPooja"? - Epsi95

3 Answers

0
votes
  • You can remove the trailing whitespaces with the strip method.
  • You can remove the all spaces (or other characters) with the replace method.
  • You can remove the all whitespaces with the split and join.
    • The split method splits a string into a list. You can specify the separator, default separator is any whitespace.
    • The join method takes all items in an iterable and joins them into one string.
  • You can remove the all whitespaces with the translate and the build-in string module

Code:

import string

input_text = """
Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
    Cras mattis purus nec aliquam placerat. 
Donec efficitur ex vel ante mattis fermentum."""

print("ORIGINAL:\n{}".format(input_text))
print("\nWITHOUT TRAILING WHITESPACES:\n{}".format(input_text.strip()))
print("\nWITHOUT SPACES:\n{}".format(input_text.replace(" ", "")))
print("\nWITHOUT ANY WHITESPACES:\n{}".format("".join(input_text.split())))
# It works only in Python3
print(
    "\nWITHOUT ANY WHITESPACES:\n{}".format(
        input_text.translate(str.maketrans("", "", string.whitespace))
    )
)

Output:

>>> python3 test.py 
ORIGINAL:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
    Cras mattis purus nec aliquam placerat. 
Donec efficitur ex vel ante mattis fermentum.

WITHOUT TRAILING WHITESPACES:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
    Cras mattis purus nec aliquam placerat. 
Donec efficitur ex vel ante mattis fermentum.

WITHOUT SPACES:

Loremipsumdolorsitamet,consecteturadipiscingelit.
Crasmattispurusnecaliquamplacerat.
Donecefficiturexvelantemattisfermentum.

WITHOUT ANY WHITESPACES:
Loremipsumdolorsitamet,consecteturadipiscingelit.Crasmattispurusnecaliquamplacerat.Donecefficiturexvelantemattisfermentum.

WITHOUT ANY WHITESPACES:
Loremipsumdolorsitamet,consecteturadipiscingelit.Crasmattispurusnecaliquamplacerat.Donecefficiturexvelantemattisfermentum.
0
votes

If you just want to remove leading and trailing whitespace, you can use the accurately-named strip function.

If you want to remove all spaces, you can use the replace function to replace spaces (" ") with the empty string ("").

If you need to remove whitespace other than spaces (e.g. tabs, newlines), you can do so using the re.sub function with a regex of r'\s'. However this is arguably not a "string function" as your question requests (depending what you mean by that).

0
votes

Try replacing whitespace,

string.replace(' ','')