Home
Map
String strip ExamplesUse the strip, lstrip and rstrip methods. Handle whitespace and other characters.
Python
This page was last reviewed on Jan 11, 2022.
Strip. In Python we have many string manipulation methods. These handle most common requirements. To handle whitespace, strip() is useful.
With strip, we remove certain characters (such as whitespace) from the left and right parts of strings. We invoke lstrip, rstrip and the versatile strip().
string Punctuation
First example. Here we invoke the lstrip, rstrip and strip methods. Strip, with no argument, removes leading and trailing whitespace.
Info With no argument, lstrip removes whitespace at the start of the string. The L stands for left.
And With no argument, rstrip removes whitespace at the end. This is the right side. If no whitespace is present, nothing happens.
# Has two leading spaces and a trailing one. value = " a line " # Remove left spaces. value1 = value.lstrip() print("[" + value1 + "]") # Remove right spaces. value2 = value.rstrip() print("[" + value2 + "]") # Remove left and right spaces. value3 = value.strip() print("[" + value3 + "]")
[a line ] [ a line] [a line]
Characters. Strip can be used for more than whitespace. Try passing an argument to it. Strip will remove all characters found in the argument string that lead, or end the string.
Note Strip() does not match substrings—it treats the argument as a set of characters. Here we specify all digits and some punctuation.
Tip Lstrip and rstrip can also be used with an argument. Their behavior changes in the same way as strip.
# Has numbers on left and right, and some syntax. value = "50342=Data,231" # Strip all digits. # ... Also remove equals sign and comma. result = value.strip("0123456789=,") print(result)
Data
Strip with dictionary. Strip can be combined with methods like lower() to preprocess keys for a dictionary. So we can look up the string "CAT" with any leading or trailing spaces.
Dictionary
Detail With lower() we can treat "cat" and "CAT" and "Cat" the same. This example is not optimally fast. But it works.
String lower, upper
def preprocess(input): # String and lowercase the input. return input.strip().lower() # Create a new dictionary. lookup = {} # Use preprocess to create key from string. # ... Use key in the dictionary. lookup[preprocess(" CAT")] = 10 # Get value from dictionary with preprocessed key. print(lookup[preprocess("Cat ")])
10
Benchmark, lstrip. How important are the arguments you pass to strip, lstrip and rstrip? Should you remove unneeded characters from the string argument to improve performance?
Version 1 This version calls lstrip with more chars than are needed to perform its internal logic.
Version 2 This version of the code just uses the needed characters, "012," to perform the operation.
Result I tested and no benefit from minimizing the argument. My modifications yielded no speedups.
import time # Input data. s = "100200input" print(s.lstrip("0123456789")) print(s.lstrip("012")) # Time 1. print(time.time()) # Version 1: specify all digits. i = 0 while i < 10000000: result = s.lstrip("0123456789") i += 1 # Time 2. print(time.time()) # Version 2: specify needed digits. i = 0 while i < 10000000: result = s.lstrip("012") i += 1 # Time 3. print(time.time())
input input 1380915621.696 1380915626.524 [With all digits: 4.83 s] 1380915631.384 [With needed digits: 4.86 s]
Split. The strip method is commonly used before calling another method, such as split. The split method works better when no leading, or trailing, whitespace is found.
And These spaces are often not helpful in processing. So strip often is used before split.
String split
A review. Strip, and its friends lstrip and rstrip, aids in preprocessing string data. This is helpful. It simplifies using methods later in your program, like split or even custom ones.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jan 11, 2022 (edit link).
Home
Changes
© 2007-2024 Sam Allen.