Python Forum

Full Version: Where does string.punctuation come from?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Really, really basic question here...

With regard to this snippet:

>>> import string
>>> string.punctuation
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
Where did the third line come from? It looks like a file has been imported: string.punctuation. How does the computer know where to find that file? No path was given, no website given as to where to find it, etc.

Or... when I installed Python on my PC, which I think I did the start of this journey, was string.punctuation part of that installation?

What's the difference between string.punctuation and a "library" (or is this such a library)?
if you download the source code, it is defined in Python-3.7.4/Lib/string.py:
Quote:punctuation = r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
string module is part of standard python library. i.e. it comes with python installation together with many more

There is nice Python3 module of the week tutorial/overview, written by Doug Hellmann

It's worth to mention that with time many of the functions in string module were implemented as methods of str object and now it's more convenient to use this methods instead of importing string module. Of course for constants you still need to use the module.

string.punctuation is one of several pre-defined constants in this module
string is part of the The Python Standard Library.
string — Common string operations look at link to code Lib/string.py

Make my own and add 999,you see that's it really simple this is all code that load that line.
# my_punctuation.py
punctuation = r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~999"""
Use:
E:\div_code
λ ptpython
>>> import my_punctuation

>>> my_punctuation.punctuation
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~999'
(Sep-16-2019, 03:12 PM)Mark17 Wrote: [ -> ]It looks like a file has been imported:
You did import a module when you called
>>> import string
(Sep-16-2019, 03:12 PM)Mark17 Wrote: [ -> ]How does the computer know where to find that file?
Python has a predefined series of locations to load modules.
Python first searches the directory the file is ran in. Then searches the PYTHONPATH. Then python searches the standard library directories. Then python searches for directories in a .pth file.

A module is a set of functions, types, classes, ... put together in a common namespace.
A library is a set of modules which makes sense to be together and that can be used in a program or another library.
A package is a unit of distribution that can contain a library or an executable or both. It's a way to share your code with the community.
More info on all that here.
Thanks everyone!