我需要获取x字符字符串的前n个字符,其中,n = x - len(endstring)
并且endstring
可以具有从0到x的任意数量的字符。
是否有更干净,更好的性能明智的方式在Python中做到这一点:
string[:len(string) - len(endstring)]
?
要点是len(string)
看起来有点多余,但是考虑endstring
到0个字符,这似乎是“最平滑的”解决方案?
您可以string[:-len(endstring) or None]
代替。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句