[Python-ideas] str.startswith taking any iterator instead of just tuple

Alexander Heger python at 2sn.net
Sun Jan 5 20:02:29 CET 2014


> People have mentioned use cases for iterating strings in this thread. And it's easy to think of more. There are all kinds of algorithms that treat strings as sequences of characters. Sure, many of these functions are already methods on str or otherwise built into the stdlib, but that just means they're implemented by iterating the string storage in C with a loop around "*++s". And if you want to extend that set of builtins with similar functions, how else would you do it but with a "for ch in s" loop? (Well, you could "for ch in list(s)", but that's still treating strings as iterables.) For example, many people are asked to write a rot13 function in one of their first classes. How would you write that if strings weren't iterables? There's no way a regex is going to help you here, unless you wanted to do something like using re.sub('.') as a convoluted and slow way of writing map.

whereas the issue seems now settled, you could use explicit functions
like str.iter(), str.codepoints(), str.substr(), ...


More information about the Python-ideas mailing list