Trim specific leading and trailing characters from a string

前端 未结 2 1429
旧时难觅i
旧时难觅i 2020-12-09 09:13

I have a string that contains a path

str = \"/example/path/with/different/trailing/delimiter\\\"

and I want to trim the leading and traili

相关标签:
2条回答
  • 2020-12-09 09:51

    Example of strip() in action; in this case, removing a leading plus sign:

    In [1]: phone_number = "+14158889999"
    
    In [2]: phone_number.strip('+')
    Out[2]: '14158889999'
    
    0 讨论(0)
  • 2020-12-09 10:08

    I believe strip is the pythonic way. It is usually the case when there is a builtin function.

    There are a few builtin path manipulators available in the os library. You might want to use them if one of the manipulators is a match for your use case.

    0 讨论(0)
提交回复
热议问题