N
News123
I wondered about a potentially nicer way of removing a prefix of a
string if it exists.
Here what I tried so far:
def rm1(prefix,txt):
if txt.startswith(prefix):
return txt[len(prefix):]
return txt
for f in [ 'file:///home/me/data.txt' , '/home/me/data.txt' ]:
# method 1 inline
prefix = "file://"
if f.startswith(prefix):
rslt = f[len(prefix):]
else
rsl = f
# method 2 as function
rslt = rm1('file://',f)
Is there any nicer function than above rm1()?
Is there any nicer inline statement rhan my method 1' ?
string if it exists.
Here what I tried so far:
def rm1(prefix,txt):
if txt.startswith(prefix):
return txt[len(prefix):]
return txt
for f in [ 'file:///home/me/data.txt' , '/home/me/data.txt' ]:
# method 1 inline
prefix = "file://"
if f.startswith(prefix):
rslt = f[len(prefix):]
else
rsl = f
# method 2 as function
rslt = rm1('file://',f)
Is there any nicer function than above rm1()?
Is there any nicer inline statement rhan my method 1' ?