Oops, I just realized you meant strip instead of split, so here's an itertools.takewhile solution:
from itertools import takewhile
def lstripped(s):
return ''.join(takewhile(str.isspace, s))
def rstripped(s):
return ''.join(reversed(tuple(takewhile(str.isspace, reversed(s)))))
def stripped(s):
return lstripped(s), rstripped(s)
The polyfill for itertools.takewhile is the following:
def takewhile(predicate, iterable):
# takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
for x in iterable:
if predicate(x):
yield x
else:
break
Answer from Shashank on Stack OverflowTutorialspoint
tutorialspoint.com › python › string_strip.htm
Python String strip() Method
Following is the syntax for Python ... as the argument to the method, the return value will be the string with the leading and trailing occurrences of the letter stripped....
W3Schools
w3schools.com › python › ref_string_strip.asp
Python String strip() Method
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... The strip() method removes any leading, and trailing whitespaces.
06:26
Python String Methods - upper() lower() strip() find() replace() ...
07:01
Python Strip String Method - YouTube
05:35
Make text processing easier with .strip() - YouTube
06:27
Master Python String Functions: strip, lstrip, rstrip Explained!
08:27
What Does Strip Do In Python - YouTube
Programiz
programiz.com › python-programming › methods › string › strip
Python String strip()
... chars - specifies the set of ... all leading and trailing whitespaces are removed from the string. The method returns a string after removing both leading and trailing spaces/characters....
Top answer 1 of 2
3
Oops, I just realized you meant strip instead of split, so here's an itertools.takewhile solution:
from itertools import takewhile
def lstripped(s):
return ''.join(takewhile(str.isspace, s))
def rstripped(s):
return ''.join(reversed(tuple(takewhile(str.isspace, reversed(s)))))
def stripped(s):
return lstripped(s), rstripped(s)
The polyfill for itertools.takewhile is the following:
def takewhile(predicate, iterable):
# takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
for x in iterable:
if predicate(x):
yield x
else:
break
2 of 2
1
I'm probably taking your words literally - but if you want to get only the whitespace in your string, then isn't list comprehension the way to go ?
In [112]: x
Out[112]: '\n\ttext goes here'
In [113]: ''.join([i for i in x if not i.isalnum()]).replace(" ",'')
Out[113]: '\n\t'
Mimo
mimo.org › glossary › python › string-strip
Python string.strip(): Syntax, Usage, and Examples
Start your coding journey with Python. Learn basics, data types, control flow, and more ... If you don’t pass any arguments, it removes all leading and trailing whitespace. If you pass a string to chars, it removes all characters found in that string from both ends. The original string remains unchanged; strip() returns ...
Codecademy
codecademy.com › docs › python › strings › .strip()
Python | Strings | .strip() | Codecademy
April 23, 2025 - If omitted, all types of leading and trailing whitespace (spaces, tabs, newlines, etc.) are removed by default. ... Returns a new string with the specified leading and trailing characters removed. Note: .strip...
Python Academy
python-academy.org › functions › strip
strip — Python Function Reference
strip · Return a copy of the string with the leading and trailing characters removed. Python 3.13 · 1 · str.strip([chars]) chars · The characters to be removed (defaults to whitespace). Examples · Python 3.13 ·
Scaler
scaler.com › home › topics › python string strip() method
Python String strip() Method - Scaler Topics
February 22, 2024 - The strip() function returns a new string by replacing a few or no characters from the old string according to the specified functionality.
Address: 5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
Python Reference
python-reference.readthedocs.io › en › latest › docs › str › strip.html
strip — Python Reference (The Right Way) 0.1 documentation
Returns a copy of the string with the leading and trailing characters removed. ... Optional. String specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix; rather, all combinations of its values ...