[Python-ideas] New explicit methods to trim strings

Steven D'Aprano steve at pearwood.info
Sun Mar 31 20:08:37 EDT 2019


Thank you, this is a simple, unambiguous proposal which meets a real need
and will help prevent a lot of wasted developer time misusing [lr]strip
and then reporting it as a bug: remove a single prefix or suffix.

This is a useful string primitive provided by other modern languages and 
libraries, including Go, Ruby, Kotlin, and the Apache StringUtils Java 
library:

https://golang.org/pkg/strings/#TrimPrefix
https://golang.org/pkg/strings/#TrimSuffix

https://ruby-doc.org/core-2.5.1/String.html#method-i-delete_prefix
https://ruby-doc.org/core-2.5.1/String.html#method-i-delete_suffix

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/remove-prefix.html
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/remove-suffix.html

https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#removeStart-java.la$
https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#removeEnd-java.lang$


Regarding later proposals to add support for multiple affixes, to 
recursively delete the affix repeatedly, and to take an additional 
argument to limit how many affixes will be removed: YAGNI.

Let's not over-engineer this to be something which is ambigious and
complex. We can add more complexity later, if and when practical
experience suggests:

(1) that multiple affixes actually is useful in practice, not just a
"Wouldn't It Be Cool???" feature; and

(2) a consensus as to how to handle ambiguous cases.

Until then, let's keep it simple: methods to remove a *single* prefix or 
suffix, precisely as given. Anything else is YAGNI and is best left for 
the individual programmer.



-- 
Steven


More information about the Python-ideas mailing list