The string module contains a set of useful constants, such as ascii_letters and digits, and the module is often still imported for that reason.
Videos
The string module contains a set of useful constants, such as ascii_letters and digits, and the module is often still imported for that reason.
If you see a import string but never see string.something, someone just forgot to remove an unused import.
While there did use to be some things in string that are now standard methods of str objects, you still had to either
- prefix them with
string.after importing the library, or - use
from string import <whatever>syntax.
Typically, the only times you'll see something properly imported but never "explicitly used" are from __future__ import with_statement or the like - the forwards/backwards compatability triggers used by Python for new language features.
Generally you don't need to import string module as the class is already in builtins. However, there are several constants that are in the string module that are not built in, that can be usefull.
There isn't really a need these days. You most likely shouldn't need to ever use import string.
You can find a list here of the components inside of the string library, but as it says most have been added into the rest of the standard library now. It's just the way that it used to be set up in Python many years ago. string mostly has a bunch of constants now.
From the comments in the source file
A collection of string operations (most are no longer used).
Warning: most of the code you see here isn't normally used nowadays. Beginning with Python 1.6, many of these functions are implemented as methods on the standard string object. They used to be implemented by a built-in module called strop, but strop is now obsolete itself.
Here is how to import a string as a module (Python 2.x):
Copyimport sys,imp
my_code = 'a = 5'
mymodule = imp.new_module('mymodule')
exec my_code in mymodule.__dict__
In Python 3, exec is a function, so this should work:
Copyimport sys,imp
my_code = 'a = 5'
mymodule = imp.new_module('mymodule')
exec(my_code, mymodule.__dict__)
Now access the module attributes (and functions, classes etc) as:
Copyprint(mymodule.a)
>>> 5
To ignore any next attempt to import, add the module to sys:
Copysys.modules['mymodule'] = mymodule
imp.new_module is deprecated since python 3.4, but it still works as of python 3.9
imp.new_module was replaced with importlib.util.module_from_spec
importlib.util.module_from_spec is preferred over using
types.ModuleTypeto create a new module as spec is used to set as many import-controlled attributes on the module as possible.importlib.util.spec_from_loader uses available loader APIs, such as
InspectLoader.is_package(), to fill in any missing information on the spec.
these module attributes are __builtins__ __doc__ __loader__ __name__ __package__ __spec__
Copy
import sys, importlib.util
def import_module_from_string(name: str, source: str):
"""
Import module from source string.
Example use:
import_module_from_string("m", "f = lambda: print('hello')")
m.f()
"""
spec = importlib.util.spec_from_loader(name, loader=None)
module = importlib.util.module_from_spec(spec)
exec(source, module.__dict__)
sys.modules[name] = module
globals()[name] = module
# demo
# note: "if True:" allows to indent the source string
import_module_from_string('hello_module', '''if True:
def hello():
print('hello')
''')
hello_module.hello()
ยป pip install strings