python - Function to return the stripped whitespace of a string -


is there existing function in python works .strip()/.lstrip()/.rstrip() do, instead, returns stripped whitespace rather resulting stripped string?

namely:

test_str = '\n\ttext goes here' test_str.lstrip() # yields 'text goes here' test_str.lwhite() # yields '\n\t' 

where .white(), .lwhite(), , .rwhite() functions i'm hoping exist. otherwise i'll have make regex , captured groups:

^(\s*).*(\s*)$    .white() ^(\s*)            .lwhite() (\s*)$            .rwhite() 

to give better example, python has .strip() methods remove whitespace @ start , end of given string , return stripped string. same python's .lstrip() , .rstrip() methods beginning , ends respectively.

i'm looking way return whitespace stripped off ends of string. string following...

sample = '\n\t string\t \n \ta sample\t!\n' 

...i'd want '\n\t ' returned beginning version, '\n' returned ending version, or both in list returned full version.

thanks all!

oops, realized meant strip instead of split, here's 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 itertools.takewhile following:

def takewhile(predicate, iterable):     # takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4     x in iterable:         if predicate(x):             yield x         else:             break 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -