1
votes

I was stripping a file name in python for routing purposes and I was getting some unexpected behavior with the python strip function. I've read the docs and searched online but have not been able to find an explanation for the following behavior:

"Getting-Started.md".strip('.md')
 Out[29]: 'Getting-Starte'

But if it is any other character aside from 'd' to the left of the period, it works properly:

"Getting-StarteX.md".strip('.md')
 Out[30]: 'Getting-StarteX'

It seems like there is something similar to a mirroring going on 'd. md'. I'm doing a double strip to get by this for now, but I was just curious of why this occurs.

Thank you.

2

2 Answers

8
votes

strip() would strip all the characters provided in the argument - in your case ., m and d.

Instead, you can use os.path.splitext():

import os
os.path.splitext("Getting-StarteX.md")[0]
0
votes

If there is only one ".md" appearing at the end of the testing string, you can also use

"Getting-Started.md".split('.md')[0]

Thanks @Carpetsmoker remind me the assumption.