My string module doesn't have maketrans or translate functions

L

Lamb

Hi all,
I'm really new to python and trying to figure out the basic rule and settings of it. I'm using python 3.3 and I was trying this code in python:

import string
s = "string. With. Punctuation?"
out = s.translate(string.maketrans("",""), string.punctuation)

And I got the following error:
Traceback (most recent call last):
File "C:/Python33/trials/ls.py", line 5, in <module>
out = s.translate(string.maketrans("",""), string.punctuation)
AttributeError: 'module' object has no attribute 'maketrans'

So I used the following to see functions inside string
e=dir(string)['ChainMap', 'Formatter', 'Template', '_TemplateMetaclass', '__builtins__','__cached__', '__doc__', '__file__', '__initializing__', '__loader__', '__name__', '__package__', '_re', '_string', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords', 'digits', 'hexdigits', 'octdigits', 'printable', 'punctuation', 'whitespace']

It doesn't have translate(), maketrans(), rstrip() and a whole lot of otherthings. What's the problem?

Thanks!
 
L

Lamb

Thanks! It worked! But why didn't I see functions : translate(), maketrans(), rstrip(), etc. listed when I called print(dir(string))?
 
L

Lamb

Thanks! It worked! But why didn't I see functions : translate(), maketrans(), rstrip(), etc. listed when I called print(dir(string))?
 
C

Chris Angelico

Thanks! It worked! But why didn't I see functions : translate(), maketrans(), rstrip(), etc. listed when I called print(dir(string))?

Because they're not in the string module any more - they're methods on
str (and bytes). Try checking out dir(str) - str being the class of
your basic Unicode string now, rather than being the type of a string
of bytes - and you should see them all.

ChrisA
 
M

Mark Lawrence

Hi all,
I'm really new to python and trying to figure out the basic rule and settings of it. I'm using python 3.3 and I was trying this code in python:

import string
s = "string. With. Punctuation?"
out = s.translate(string.maketrans("",""), string.punctuation)

And I got the following error:
Traceback (most recent call last):
File "C:/Python33/trials/ls.py", line 5, in <module>
out = s.translate(string.maketrans("",""), string.punctuation)
AttributeError: 'module' object has no attribute 'maketrans'

So I used the following to see functions inside string
e=dir(string)['ChainMap', 'Formatter', 'Template', '_TemplateMetaclass', '__builtins__', '__cached__', '__doc__', '__file__', '__initializing__', '__loader__', '__name__', '__package__', '_re', '_string', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords', 'digits', 'hexdigits', 'octdigits', 'printable', 'punctuation', 'whitespace']

It doesn't have translate(), maketrans(), rstrip() and a whole lot of other things. What's the problem?

Thanks!

The string module is effectively dead. Many functions were deprecated
back in 2.6, maketrans in 3.1, just use string methods instead.
 
S

Steven D'Aprano

The string module is effectively dead.

It's not dead, it's pining for the fjords.

But seriously, the string module holds a collection of useful string
constants and the Template class.
 

Members online

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top