rstrip()

J

Jason Friedman

$ python
Python 2.6.4 (r264:75706, Dec 7 2009, 18:43:55)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.'x.vs'

I expected 'x.vsd' as a return value.
 
N

Novocastrian_Nomad

$ python
Python 2.6.4 (r264:75706, Dec  7 2009, 18:43:55)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
'x.vs'

I expected 'x.vsd' as a return value.

One way to achieve the desired result:
'x.vsd-dir'.split('-')[0]
 
N

News123

Jason said:
$ python
Python 2.6.4 (r264:75706, Dec 7 2009, 18:43:55)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.'x.vs'

I expected 'x.vsd' as a return value.

This is kind of similiar to the question, that I posted recently.
"nicer way to remove prefix of a string if it exists"



if you want to remove '-dir' at the end of the string if it exists and
leave the string as it is if it is not followed by '-dir',

then you could do:

def rmv_suffix(suffix,txt):
if txt.endswith(suffix):
return txt[:-len(suffix)]
return txt


the other solution would involve regular expressions:


import re
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top