TheDeveloperBlog.com


Python Strip Examples

Python Strip Examples

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.

Based on:

Python 3

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. How important are the arguments you pass to strip, lstrip and rstrip? Should you remove unneeded characters from the string argument to improve performance?

In my spare time: I tested. I found no benefit from minimizing the argument. My modifications yielded no speedups.

So: Trying to optimize a program by simplifying arguments to strip, lstrip and rstrip seems like a waste of time.

Result: For an argument with 10 chars, performance was about the same (actually, slightly better) than with 3 chars.

Python program that times lstrip

import time

# Input data.
s = "100200input"

print(s.lstrip("0123456789"))
print(s.lstrip("012"))

# Time 1.
print(time.time())

# Specify all digits.
i = 0
while i < 10000000:
    result = s.lstrip("0123456789")
    i += 1

# Time 2.
print(time.time())

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

Split

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.


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.