[Python-ideas] New explicit methods to trim strings

Barry Scott barry at barrys-emacs.org
Wed Apr 3 14:03:01 EDT 2019


Use "without" as the action picking up on "with" as in startswith, endswith:

	new_string = a_string.withoutprefix( prefix )
	new_string = a_sring.withoutsuffix( suffix )

And since we have "replace" "remove" would also seem
obvious.

	new_string = a_string.removeprefix( prefix )
	new_string = a_sring.removesuffix( suffix )

I know that some commented that remove sounds like its inplace.
But then so does replace.

Would "replacesuffix" and "replaceprefix" work? I'd default
the "replacement" to the empty string "".

	new_string = a_string.replaceprefix( old_prefix, replacement )
	new_string = a_sring.replacesuffix( old_suffix, replacement )

Barry



More information about the Python-ideas mailing list