String.strip() in Python

Question:

While learning about python, I came upon this code, which takes a text file, splits each line into an array, and inserts it into a custom dictionary, where the array[0] is the key and array[1] is the value:

my_dict = {}

infile = open("file.txt")
for line in infile:
    #line = line.strip() 
    #parts = [p.strip() for p in line.split("t")]
    parts = [p for p in line.split("t")]
    my_dict[parts[0]] = parts[1]
    print line

for key in my_dict:
    print "key: " + key + "t" + "value " + my_dict[key]

I ran the program with the commented lines off and on and I got the same result. (of course replacing the second commented line with the line below it).It seems to me that doing a strip() is optional. Is it better practice to leave it in?

Asked By: Rhs

||

Answers:

If you can comment out code and your program still works, then yes, that code was optional.

.strip() with no arguments (or None as the first argument) removes all whitespace at the start and end, including spaces, tabs, newlines and carriage returns. Leaving it in doesn’t do any harm, and allows your program to deal with unexpected extra whitespace inserted into the file.

For example, by using .strip(), the following two lines in a file would lead to the same end result:

 footbar n
footbarn

I’d say leave it in.

Answered By: Martijn Pieters

In this case, you might get some differences. Consider a line like:

"footbar "

In this case, if you strip, then you’ll get {"foo":"bar"} as the dictionary entry. If you don’t strip, you’ll get {"foo":"bar "} (note the extra space at the end)

Note that if you use line.split() instead of line.split('t'), you’ll split on every whitespace character and the “striping” will be done during splitting automatically. In other words:

line.strip().split()

is always identical to:

line.split()

but:

line.strip().split(delimiter)

Is not necessarily equivalent to:

line.split(delimiter)
Answered By: mgilson

strip removes the whitespace from the beginning and end of the string. If you want the whitespace, don’t call strip.

Answered By: zmbq

No, it is better practice to leave them out.

Without strip(), you can have empty keys and values:

apples<tab>round, fruity things
oranges<tab>round, fruity things
bananas<tab>

Without strip(), bananas is present in the dictionary but with an empty string as value. With strip(), this code will throw an exception because it strips the tab of the banana line.

Answered By: Sjoerd

strip does nothing but, removes the the whitespace in your string. If you want to remove the extra whitepace from front and back of your string, you can use strip.

The example string which can illustrate that is this:

In [2]: x = "something t like     t this"
In [4]: x.split('t')
Out[4]: ['something ', ' like     ', ' this']

See, even after splitting with t there is extra whitespace in first and second items which can be removed using strip in your code.

Answered By: Senthil Kumaran
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.