3 ms·
String indexing is pretty rare. Most of them are s[0] or s[:n], where you don't need an index. Typically this is found when parsing something, eg. if line
by throwawayish 10y ago
String indexing is pretty rare. Most of them are s[0] or s[:n], where you don't need an index.
Typically this is found when parsing something, eg.
if line[0] == '#':
# skip comment lines
continue
Or
prefixes = {...}
for line in lines:
prefix, remainder = line[:4], line[4:]
if prefix not in prefixes:
raise ValueError('Invalid prefix ' + prefix)
parsed = prefixes[prefix](remainder)
- Manishearth 10y agoDon't you need to index for `s[:n]`? You need to find the byte position of code point n. In current python this isn't a problem because the strings don't use a variable-width encoding, but we're talking about making Python use utf8 :)