Python Strip Examples

Use the strip, lstrip and rstrip methods. Handle whitespace and other characters.

Strip. String manipulation is complex. In Python we have many 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().

Often, strings have unwanted leading and trailing chars. Spaces are common. With lstrip, rstrip and strip, we remove these. Strip, with no argument, removes leading and trailing whitespace.

Lstrip: With no argument, lstrip removes whitespace at the start of the string. The L stands for left.

Rstrip: With no argument, rstrip removes whitespace at the end. This is the right side. If no whitespace is present, nothing happens.

Tip: When a string is argument is passed to any of these strip methods, only characters in that set are removed.

Python program that uses lstrip, rstrip, strip # 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 + "]") Output [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.

Python program that uses strip with argument # 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) Output 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

Lower: With lower() we can treat "cat" and "CAT" and "Cat" the same. This example is not optimally fast. But it works.

Lower, Upper
Python program that uses strip, lower, dictionary 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 ")]) Output 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.

Python program that times lstrip 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()) Output 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.


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.Strings

For program design, you don't need to do everything at once. Instead, split up the task into individual steps. In your thinking, isolate each step. Strip out unwanted complexity.
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to