In programming, whitespaces refer to any characters used for formatting or spacing purposes but not visible on the screen. They are commonly used to separate words or elements within a line of code or make it more readable.
The most common whitespaces are spaces, tabs, and line breaks. Spaces separate words or elements within a line of code, while tabs are used to indent code to show hierarchy or structure. Line breaks are used to separate lines of code.
In programming languages, such as Python, the use of whitespaces is significant because it determines the structure of the code. For example, indentation indicates the beginning and end of a code block in Python rather than curly braces, as in other languages.
To remove whitespaces in Python, we can use the strip()
, rstrip()
, and lstrip()
methods. We will discuss them one by one.
strip()
methodThe strip()
method removes leading and trailing whitespace characters (spaces, tabs, and newline characters) from a string. It delivers an output of a new string with no whitespace character, but the original is not modified.
# Define a string with leading and trailing whitespacetext = " Hello, the string has whitespaces "# Use strip() to remove the whitespacestripped_text = text.strip()# Print the original string and the stripped string to compare themprint("Original string: {text}".format(text= text))print("Stripped string: {stripped_text}".format(stripped_text= stripped_text))
Line 2: We define a text
with a string having whitespaces at the front and at the end.
Line 5: Using the strip()
method, we remove the whitespace and save the result in a new variable stripped_text
.
Line 8: We print the original string to the console.
Line 9: We also print the stripped text to the console to compare it with the original text. The stripped text does not contain any whitespace characters.
rstrip()
methodWith the help of the rstrip()
method, we can easily remove the trailing whitespace characters (spaces, tabs, and newline characters) from a string. It returns an output of a new string with no whitespace character, but the original text is not modified which is similar to strip()
method.
# Define a string with leading and trailing whitespacetext = "Hello, whitespaces at the end "# Use strip() to remove the whitespacerstrip_text = text.rstrip()# Print the original string and the stripped string to compare themprint("Original string: {text}".format(text= text))print("Stripped string: {rstrip_text}".format(rstrip_text= rstrip_text))
Line 2: We define a text
with a string having whitespaces at the end.
Line 5: Using the rstrip()
method, we remove the whitespace and save the result in a new variable rstrip_text
.
Line 8: We print the original string to the console.
Line 9: We also print the stripped text to the console to compare it with the original text. The stripped text does not contain any whitespace characters at the end.
lstrip()
methodSimilarly, the lstrip()
method removes only the leading whitespace characters from a string. n is similar to the strip() method. It returns an output of a new string with no whitespace character, but the original text is not modified which is similar to strip()
method.
# Define a string with leading and trailing whitespacetext = " Hello, whitespaces at the beginning!"# Use strip() to remove the whitespacelstrip_text = text.lstrip()# Print the original string and the stripped string to compare themprint("Original string: {text}".format(text= text))print("Stripped string: {lstrip_text}".format(lstrip_text= lstrip_text))
Line 2: We define a text
with a string having whitespaces at the beginning.
Line 5: Using the lstrip()
method, we remove the whitespace and save the result in a new variable lstrip_text
.
Line 8: We print the original string to the console.
Line 9: We also print the stripped text to the console to compare it with the original text. The stripped text does not contain any whitespace characters.
It is important to note that while whitespaces are not visible on the screen, they can affect how the computer interprets and executes code. Therefore, it's essential to use them appropriately to ensure the code is correct and functional.
Free Resources