BASIC STRING HANDLING

Python supports the basic text extraction from strings supported by the Perl substr() function using slices on a string, since strings are just another type of sequence. For the other functions traditionally supported through internal functions such as split() and chomp() we need to use the string module. We'll look in detail at the specifics later, but a quick list of the compatible functions are shown in Table 10.1.

Table 10.1. Perl and Python functions for handling strings
Perl function Python equivalent
substr(text, offset) text[offset:len]
index(text, substr, start) string.index(text, substr, start)
rindex(text, substr, start) string.index(text, substr, start)
split(expr, text, max) string.split(text, expr, max)
join(expr, ...

Get Perl To Python Migration now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.