How python trim whitespace in a string, It is our target to learn today. For a beginner it raises some questions.
First of all, what is a whitespace?
In the sense of computer programming whitespace or in other words space is also a character but visible to writer as a blank space. White space can be created by pressing keys ‘Spacebar key’ or ‘Tab key’. Spacebar key creates 1 single instance of whitespace but the Tab key creates 4 consecutive instances of whitespace.
How can python trim whitespace?
Python trim whitespace doesn’t means that to remove all the whitespaces found in a string. Instead it means to remove whitespaces from start and end of a python string. i.e
>>>" I love python string. " will becomes >>>"I love python string."
To complete our target task we have python strip function. Strip function will remove all the whitespaces from start and end of the string.
>>>print(" hi spaces ".strip()) "hi spaces" >>>print("hi spaces\t".strip()) "hi spaces"
Even python strip sibling functions are exist to remove left only or right only whitespaces.
>>>print(" hi spaces ".lstrip()) "hi spaces " >>>print("hi spaces\t".rstrip()) " hi spaces"