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.

Answer from Carl Younger on Stack Overflow
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ string.html
Common string operations โ€” Python 3.14.3 documentation
Source code: Lib/string/__init__.py String constants: The constants defined in this module are: Custom String Formatting: The built-in string class provides the ability to do complex variable subst...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-string-module
Python String Module - GeeksforGeeks
July 23, 2025 - From predefined sets of characters (such as ASCII letters, digits and punctuation) to useful functions for string formatting and manipulation, the string module streamlines various string operations that are commonly encountered in programming. Note: The Python String module is a part of the standard Python library, so we do not need to explicitly install it.
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-string-module
Python String Module | DigitalOcean
August 3, 2022 - import string # string module constants ... 0123456789 0123456789abcdefABCDEF !"#$%&'()*+,-./:;?@[\]^_`{|}~ Python string module contains a single utility function - capwords(s, sep=None)....
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_ref_string.asp
Python String Methods
Python has a set of built-in methods that you can use on strings.
๐ŸŒ
LaunchCode
education.launchcode.org โ€บ lchs โ€บ chapters โ€บ strings โ€บ string-module.html
7.10. The string Module โ€” LaunchCode's LCHS documentation
Weโ€™ve talked before about string methods. The Python language gives us another means of working with string data. The Python string module provides several constants that are useful for checking to see if a character, slice, or string contains letters, digits, symbols, etc.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_module_string.asp
Python string Module
Use it to access predefined sets of characters (letters, digits, punctuation) or to create custom string formatters using the Template class. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us ...
๐ŸŒ
O'Reilly
oreilly.com โ€บ library โ€บ view โ€บ python-standard-library โ€บ 0596000960 โ€บ ch01s07.html
The string Module - Python Standard Library [Book]
May 10, 2001 - The string Module The string module contains a number of functions to process standard Python strings, as shown in Example 1-51.Example 1-51. Using the string ModuleFile:... - Selection from Python Standard Library [Book]
Author ย  Fredrik Lundh
Published ย  2001
Pages ย  304
Find elsewhere
๐ŸŒ
Real Python
realpython.com โ€บ ref โ€บ stdlib โ€บ string
string | Python Standard Library โ€“ Real Python
The Python string module provides a collection of string constants and utility functions for common string operations.
๐ŸŒ
Real Python
realpython.com โ€บ lessons โ€บ string-module
string Module (Video) โ€“ Real Python
This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas. ... In this lesson, youโ€™ll learn about the string module. This module will help you quickly access some string constants.
Published ย  April 21, 2020
๐ŸŒ
LaunchCode
education.launchcode.org โ€บ data-analysis โ€บ chapters โ€บ strings โ€บ string-module.html
8.10. The string Module โ€” Data Analysis documentation
Weโ€™ve talked before about string methods. The Python language gives us another means of working with string data. The Python string module provides several constants that are useful for checking to see if a character, slice, or string contains letters, digits, symbols, etc.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ python string module
Python String Module - Scaler Topics
December 19, 2022 - Breaking down the heading of this article - Python string module, let's talk about the module first. Some predefined code that performs certain tasks and can be used in other Python programs, by importing it, is called a module. The string here is, not the string class that you have read about.
๐ŸŒ
Python Module of the Week
pymotw.com โ€บ 2 โ€บ string
string โ€“ Working with text - Python Module of the Week
The constants in the string module can be used to specify categories of characters such as ascii_letters and digits. Some of the constants, such as lowercase, are locale-dependent so the value changes to reflect the language settings of the user.
๐ŸŒ
Hyperskill
hyperskill.org โ€บ learn โ€บ step โ€บ 15332
The string module
Hyperskill is an educational platform for learning programming and software development through project-based courses, that helps you secure a job in tech. Master Python, Java, Kotlin, and more with real-world coding challenges.
๐ŸŒ
Pynerds
pynerds.com โ€บ the-string-module-in-python
The string module in Python
January 9, 2024 - The string module in the standard library provide various tools for working with strings. It contain several constants, functions, and classes to aid
๐ŸŒ
Python
docs.python.org โ€บ 3.0 โ€บ library โ€บ string.html
string โ€” Common string operations โ€” Python v3.0.1 documentation
The string module contains a number of useful constants and classes, as well as some deprecated legacy functions that are also available as methods on strings.
Top answer
1 of 6
62

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
2 of 6
35

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.ModuleType to 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()
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-string
Python String - GeeksforGeeks
Example: In this example we are changing first character by building a new string. ... In Python, it is not possible to delete individual characters from a string since strings are immutable.
Published ย  5 days ago
๐ŸŒ
PyPI
pypi.org โ€บ project โ€บ strings
strings ยท PyPI
Python strings for humans. ... >>> from strings import string >>> s = string("Hello, World") >>> s 'Hello, World' >>> s.len() 12 >>> s.length 12 >>> s.size 12 >>> s + ", What?" Traceback (most recent call last): File "strings.py", line 27, in <module> x = s.add(", world") File "strings.py", line 20, in add return self + string(value) File "strings.py", line 23, in __add__ raise NotImplementedError("Use add instead") NotImplementedError: Use add instead >>> s.add(", What?") 'Hello, World, What?'
      ยป pip install strings
    
Published ย  Jul 11, 2013
Version ย  0.1.2