I have a string that contains a path
str = "/example/path/with/different/trailing/delimiter\"
and I want to trim the leading and trailing /
and \
. What is the best practice in Python 3?
Currently I'm using
trimmedPath = str.strip("/\\") # trimmedPath is "example/path/with/different/trailing/delimiter" as desired
Two questions:
To remove leading and trailing spaces in Java, use the trim() method. This method returns a copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.
Trim() Trim() removes the leading and trailing occurrences of whitespaces from a string and returns a new string. Each leading and trailing trim operation is stopped when a character other than a whitespace is encountered.
In the StringUtils class, we have the methods stripStart() and stripEnd(). They remove leading and trailing characters respectively.
SQL Server TRIM() Function The TRIM() function removes the space character OR other specified characters from the start or end of a string. By default, the TRIM() function removes leading and trailing spaces from a string.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With