Python Forum
Where does string.punctuation come from?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Where does string.punctuation come from?
#1
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)?
Reply
#2
if you download the source code, it is defined in Python-3.7.4/Lib/string.py:
Quote:punctuation = r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
Reply
#3
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
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'
Reply
#5
(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.
Recommended Tutorials:
Reply
#6
Thanks everyone!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  string.punctuation for languages like French or German Leo978 1 2,314 Jun-06-2020, 09:09 AM
Last Post: DeaD_EyE
  Removing punctuation from strings in lists iFunKtion 3 16,781 May-21-2017, 06:43 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020