[Python-ideas] Split, slice, join and return "syntax" for str

Clint Hepner clint.hepner at gmail.com
Sun Mar 4 13:44:20 EST 2018


> On 2018 Mar 4 , at 12:59 p, Andrés Delfino <adelfino at gmail.com> wrote:
> 
> Hi!
> 
> I was thinking: perhaps it would be nice to be able to quicky split a string, do some slicing, and then obtaining the joined string back.
> 
> Say we have the string: "docs.python.org", and we want to change "docs" to "wiki". Of course, there are a ton of simpler ways to solve this particular need, but perhaps str could have something like this:
> 
> spam = "docs.python.org"
> eggs = "wiki." + spam['.'][1:]
> print(eggs) #wiki.python.org

-1. I see no compelling reason to overload __getitem__ to provide a synonym for the split method. 

    eggs = "wiki." + spam.split('.')[1:]

Besides, you can already make such replacements more efficiently with

    eggs = spam.replace('docs', 'wiki')

or, for more complex replacements, 

    eggs = re.sub('^docs', 'wiki', spam)

--
Clint


More information about the Python-ideas mailing list