[Tutor] Table of replacements for deprecated fns from 'string'module?

Alan Gauld alan.gauld at btinternet.com
Thu Jun 14 09:43:47 CEST 2007


"Stephen McInerney" <spmcinerney at hotmail.com> wrote

> Where is there a table of replacements for the deprecated 'string' 
> fns

I'm not sure exactly what you are after but comparing these lists 
might help:

import string
dir(string)
['Template', '_TemplateMetaclass', '__builtins__', '__doc__', 
'__file__', '__name__', '_float', '_idmap', '_idmapL', '_int', 
'_long', '_multimap', '_re', 'ascii_letters', 'ascii_lowercase', 
'ascii_uppercase', 'atof', 'atof_error', 'atoi', 'atoi_error', 'atol', 
'atol_error', 'capitalize', 'capwords', 'center', 'count', 'digits', 
'expandtabs', 'find', 'hexdigits', 'index', 'index_error', 'join', 
'joinfields', 'letters', 'ljust', 'lower', 'lowercase', 'lstrip', 
'maketrans', 'octdigits', 'printable', 'punctuation', 'replace', 
'rfind', 'rindex', 'rjust', 'rsplit', 'rstrip', 'split', 
'splitfields', 'strip', 'swapcase', 'translate', 'upper', 'uppercase', 
'whitespace', 'zfill']

dir('')
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', 
'__eq__', '__ge__', '__getattribute__', '__getitem__', 
'__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', 
'__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', 
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', 
'__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 
'decode', 'encode', 'endswith', 'expandtabs', 'find', 'index', 
'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 
'isupper', 'join', 'ljust', 'lower', 'lstrip', 'replace', 'rfind', 
'rindex', 'rjust', 'rsplit', 'rstrip', 'split', 'splitlines', 
'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 
'zfill']

Notice the long list of predicate methods that largely replace the
idiom of looking for a match in the string constants. ie instead of:

if char in string.lowercase:

you can use

if char.islower():

The only missing function I can see is maketrans().
(capwords seems to be replaced with title, and the type
conversions are now in their type objects)

The list of predicates is not complete however.

But of course the string objects add a number of extra methods
too. (eg startwith, endswith, and a bunch of operators etc)

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list