strip()

"I am currently a Software Engineering student at ALX. I'm passionate about technology and enjoy conducting research to find answers on my own. I have a natural inclination to ask 'WHY' more often than 'HOW'.
"While working on projects at ALX, I have acquired a wealth of interesting and diverse knowledge about software engineering and computer science in general. Therefore, I needed a place to store and save all this information, allowing me to refer back to it whenever I forget."
The strip() method removes leading and trailing whitespace, including extra newlines. It will remove any newline characters, spaces, tabs, or other whitespace characters at the beginning and end of the string.
For example, if the original text is "\nHello world, \n How are you?\n\n", When strip() is applied, it will remove the leading and trailing newlines, resulting in "Hello world.\n How are you?".
What if you want to keep the newlines and remove only extra white or white space and tabs,
Python still has a way around it
If you want to remove only extra whitespace characters (spaces and tabs) and not newlines, you can use the rstrip() and lstrip() methods instead of strip(). Here's an example of a code that removes extra whitespace but keeps newlines intact:
def text_indentation(text):
if not isinstance(text, str):
raise TypeError('text must be a string')
punctuation_marks = ['.', '?', ':']
new_text = ""
for char in text:
new_text += char
if char in punctuation_marks:
new_text += '\n\n'
new_text = new_text.rstrip(' \t') # Remove trailing spaces and tabs
OR
new_text = new_text.rstrip(' \t').lstrip(' \t') # Removes from left and right
OR
new_text = new_text.strip(' \t') # Works ok too
print(new_text)
This way, newlines will be preserved in the modified text.
Note that rstrip(' \t') will remove whitespace characters (spaces and tabs) only from the end of the string, and lstrip(' \t') can be used to remove them from the beginning. If you want to remove whitespace characters from both ends of the string, you can use strip(' \t').



