Lstrip, rstrip and strip remove characters from the left, right and both ends of a string respectively The with statement saves you from having to call close manually. By default they remove whitespace characters (space, tabs, linebreaks, etc)
Without strip (), bananas is present in the dictionary but with an empty string as value So instead you can read the whole thing then split on spaces With strip (), this code will throw an exception because it strips the tab of the banana line.
I want to eliminate all the whitespace from a string, on both ends, and in between words I have this python code Sentence = ' hello apple ' sentence.strip() but that I was told it deletes whitespace but s = ss asdas vsadsafas asfasasgas print(s.strip()) prints out ss asdas vsadsafas asfasasgas shouldn't it be ssasdasvsadsafasasfasasgas?
3 just to add a few examples to jim's answer, according to.strip() docs Return a copy of the string with the leading and trailing characters removed The chars argument is a string specifying the set of characters to be removed If omitted or none, the chars argument defaults to removing whitespace.
However, strings in python are immutable and can't be modified, i.e The line.strip() call will not change the line object The result is a new string which is returned by the call. In short, i'd trust strip
Maybe your application cannot be reduced any further without code changes. 926 strip doesn't mean remove this substring X.strip(y) treats y as a set of characters and strips any characters in that set from both ends of x On python 3.9 and newer you can use the removeprefix and removesuffix methods to remove an entire substring from either side of the string:
(better yet, just use a list comprehension) iterating over a file object gives you lines, not words