Module_3_Notes
Module_3_Notes
Python
Programming
JUNE 12
SMVITM, Bantakal
by: Hithesh
1
Module 3: Manipulating Strings
Manipulating Strings: Working with Strings, Useful String Methods, Project:
Password Locker, Project: Adding Bullets to Wiki Markup
Reading and Writing Files: Files and File Paths, The os.path Module, The File
Reading/Writing Process, Saving Variables with the shelve Module, Saving
Variables with the print.format() Function, Project: Generating Random Quiz
Files, Project: Multiclipboard.
2
Working with Strings
String Literals
➢ Typing string values in Python code is fairly straightforward: They begin and end
with a single or double quote.
➢ But there is an issue to use a quote inside a string,
➢ Typing 'That is Alice's cat.' won’t work, because Python thinks the string ends
after Alice, and the rest (s cat.') is invalid Python code.
➢ Fortunately, there are multiple ways to type strings.
Double Quotes
➢ Strings can begin and end with double quotes, just as they do with single quotes.
➢ One benefit of using double quotes is that the string can have a single quote
character in it.
Enter the following into the interactive shell
➢ Since the string begins with a double quote, Python knows that the single
quote is part of the string and not marking the end of the string.
➢ However, if you need to use both single quotes and double quotes in the
string, you’ll need to use escape characters.
Escape Characters
➢ An escape character lets you use characters that are otherwise impossible to put
into a string.
➢ An escape character consists of a backslash (\) followed by the character you
want to add to the string. (Despite consisting of two characters, it is commonly
referred to as a singular escape character.)
➢ For example, the escape character for a single quote is \'.
➢ You can use this inside a string that begins and ends with single quotes.
3
Enter the following into the interactive shell:
Python knows that since the single quote in Bob\'s has a backslash, it is not a single
quote meant to end the string value.
The escape characters \' and \" let you put single quotes and double quotes inside
your strings, respectively.
Raw Strings
➢ You can place an r before the beginning quotation mark of a string to make it a
raw string.
➢ A raw string completely ignores all escape characters and prints any backslash
that appears in the string.
For example, type the following into the interactive shell:
4
>>>print(r'That is Carol\'s cat.')
That is Carol\'s cat.
➢ Because this is a raw string, Python considers the backslash as part of the string
and not as the start of an escape character.
➢ Raw strings are helpful if you are typing string values that contain many
backslashes.
print('Dear Alice,\n\nEve\'s cat has been arrested for catnapping, cat burglary, and
extortion.\n\nSincerely,\nBob')
5
Multiline Comments
➢ While the hash character (#) marks the beginning of a comment for the rest of
the line,
➢ a multiline string is often used for comments that span multiple lines.
The following is perfectly valid Python code
➢ The space and exclamation point are included in the character count, so 'Hello
world!' is 12 characters long, from H at index 0 to ! at index 11.
➢ If you specify an index, you’ll get the character at that position in the string.
➢ If you specify a range from one index to another, the starting index is included
and the ending index is not.
➢ That’s why, if spam is 'Hello world!', spam[0:5] is 'Hello'.
➢ The substring you get from spam[0:5] will include everything from spam[0] to
spam[4], leaving out the space at index 5.
➢ Note that slicing a string does not modify the original string.
➢ You can capture a slice from one variable in a separate variable.
7
Useful String Methods
upper()
lower()
isupper() and
islower()
8
Ex:
>>> spam = 'Hello world!'
>>> spam = spam.upper()
>>> print(spam )
'HELLO WORLD!'
➢ Note that these methods do not change the string itself but return new string
values.
➢ If you want to change the original string, you have to call upper() on the string
and then assign the new string to the variable where the original was stored.
➢ This is why you must use spam = spam.upper() to change the string in spam
instead of simply spam.upper().
➢ (This is just like if a variable eggs contains the value 10. Writing eggs + 3 does not
change the value of eggs, but eggs = eggs + 3 does.)
➢ The upper() methods are helpful if you need to make a case-insensitive
comparison.
Python String lower() method converts all uppercase characters in a string into
lowercase characters and returns it.
➢ Symbols and Numbers are ignored.
Syntax: string.lower()
Ex:
>>> spam = 'Hello World!'
>>> spam = spam.lower()
9
>>> print(spam)
'hello world!'
output:
How are you?
GREat
I feel great too.
The isupper()
➢ The isupper() method returns True if all the characters are in upper case,
otherwise False.
➢ Symbols and Numbers are ignored.
➢ It returns “True” for whitespaces but if there is only whitespace in the string
then returns “False”.
➢ It does not take any arguments, Therefore, It returns an error if a parameter is
passed.
➢ Digits and symbols return “True” but if the string contains only digits and
symbols then returns “False”
Syntax: string_name.isupper()
10
Ex:
a = "Hello World!"
b = "HELLO 123"
c = "MY NAME IS PETER"
d=““
e = “123”
print(a.isupper())
print(b.isupper())
print(c.isupper())
print(d.isupper())
print(e.isupper())
OUTPUT:
False
True
True
False
False
The islower()
➢ The islower() method returns True if all the characters are in lower case,
otherwise False.
➢ Numbers, symbols and spaces are not checked, only alphabet characters.
➢ It returns “True” for whitespaces but if there is only whitespace in the string
then returns “False”.
➢ It does not take any arguments; Therefore, it returns an error if a parameter is
passed.
11
➢ Digits and symbols return “True” but if the string contains only digits and
numbers then returns “False”.
Syntax: string.islower()
Ex:
a = "Hello World!"
b = "hello 123"
c = "my name is peter"
print(a.islower())
print(b.islower())
print(c.islower())
OUTPUT:
False
True
True
12
The isX String Methods
➢ Along with islower() and isupper(), there are several string methods that have
names beginning with the word is.
➢ These methods return a Boolean value(True/False) that describes the nature of
the string.
Here are some common isX string methods:
➢ isalpha() returns True if the string consists only of letters() and is not blank.
➢ isalnum() returns True if the string consists only of letters and numbers and is
not blank.
➢ isdecimal() returns True if the string consists only of numeric characters and is
not blank
➢ isspace() returns True if the string consists only of spaces, tabs, and newlines
and is not blank.
➢ istitle() returns True if the string consists only of words that begin with an
uppercase letter followed by only lowercase letters.
13
The startswith() and endswith() String Methods
Startswith()
The startswith() method returns True if the string starts with the specified value,
otherwise False.
Syntax:
string_name.startswith(search_string, start, end)
Parameters:
search_string: Required. The string to be searched.
start: Optional. start index of the str from where the search_string is to be searched.
end: Optional. end index of the str, which is to be considered for searching.
14
Returns :
The return value is boolean.
The functions return True if the original Sentence starts with the search_string else
False
Ex 1:
message = 'Python is fun'
# checks if the message starts with Python
print(message.startswith('Python'))
Output:
True
Ex 2:
Output:
False
endswith()
➢ endswith() function is used to check whether a given Sentence ends with
some particular string.
➢ We may use them when we want only some particular substring of the
original string to be considered for searching.
15
Parameters :
search_string : Required. The string to be searched.
start : Optional. Start index of the str from where the search_string is to be
searched.
end : Optional. End index of the str, which is to be considered for searching.
Returns :
The return value is boolean.
The functions returns True if the original Sentence ends with the
search_string else False.
Ex:
message = 'Python is fun'
# check if the message starts with Python
print(message.endswith('fun'))
Output:
True
Ex:
message = 'Python is fun'
# check if the message starts with Python
print(message.endswith('Fun'))
Output:
False
16
➢ The join() method is useful when you have a list of strings that need to
be joined together into a single string value.
➢ The join() method takes all items in an iterable and joins them into one
string.
➢ A string must be specified as the separator.
Syntax:
string.join(iterable)
Example
x = ['cats', 'rats', 'bats']
xj = ', '.join(x)
print(xj)
Output:
cats, rats, bats
Example
x = "#" .join(myTuple)
print(x)
Output:
John#Peter#Vicky
17
String split() Method
The split() method splits a string into a list.
You can specify the separator, default separator is any whitespace.
Note: When maxsplit is specified, the list will contain the specified number of
elements plus one.
Ex:
print(type(x))
Output:
['hello', 'my name', 'is Peter I am 26 years old']
<class 'list'>
Ex
txt = "apple#banana#cherry#orange"
print(x)
Output:
['apple', 'banana#cherry#orange']
<class 'list'>
18
Justifying Text with rjust(), ljust(), and center()
String rjust() Method – right justify
The rjust() method will right align the string, using a specified character (space
is default) as the fill character.
Syntax
string.rjust(length, character)
character→ Optional. A character to fill the missing space (to the left of the
string).
Default is " " (space).
Ex.
txt = "banana"
x = txt.rjust(20) # Return a 20 characters long, right justified version of the
word "banana":
print(x, "is my favorite fruit.")
Output:
txt = "banana"
x = txt.rjust(20, “O”)
print(x)
Output:
OOOOOOOOOOOOOObanana
19
Python String ljust() Method – left justify
The ljust() method will left align the string, using a specified character (space is
default) as the fill character.
Syntax
string.ljust(length, character)
Ex:
txt = "banana"
x = txt.ljust(20,'-')
print(x, "is my favourite fruit.")
Output:
txt = "banana"
x = txt.ljust(20, "O")
print(x)
Output:
bananaOOOOOOOOOOOOOO
20
Python String center() Method
The center() method will center align the string, using a specified character (space is
default) as the fill character.
Syntax
string.center(length, character)
Ex:
txt = "banana"
x = txt.center(20, "O")
print(x)
Output:
OOOOOOObananaOOOOOOO
Syntax
string.strip(characters)
21
Ex:
Output:
of all fruits banana is my favorite
Ex:
txt = ",,,,,rrttgg.....banana....rrr"
x = txt.strip(",.grt")
print(x)
Output:
banana
The rstrip() method removes any trailing characters (characters at the end of a string),
space is the default trailing character to remove.
Syntax:
string.rstrip(characters)
characters → Optional. A set of characters to remove as trailing characters.
Ex:
txt = " banana "
x = txt.rstrip() #Removes any white spaces at the end of the string:
print("of all the fruits", x, "is my favorite")
Output:
of all the fruits banana is my favorite
22
Ex:
txt = "banana,,,,,ssqqqww....."
x = txt.rstrip(",.qsw") #Removes the trailing characters
print(x)
Output:
banana
Ex:
txt = " banana "
x = txt.lstrip()
print("of all the fruits", x, "is my favorite")
Output:
of all fruits banana is my favorite
Ex
txt = ",,,,,ssaaww.....banana"
x = txt.lstrip(",.asw")
print(x)
Output:
banana
23
Copying and Pasting Strings with the pyperclip Module¶
➢ The pyperclip module has copy() and paste() functions that can send text to and
receive text from computer’s clipboard.
➢ Sending the output of your program to the clipboard will make it easy to paste it
to an email, word processor, or some other software.
➢ Note : Copy function will convert every data type to string.
Ex:
24
Project: Adding Bullets to Wiki Markup
# Adds Wikipedia bullet points to the start of each line of text on the clipboard.
import pyperclip
OUTPUT:
* Lists of animals
* Lists of aquarium life
* Lists of biologists by author abbreviation
* Lists of cultivars
25
involve the pyperclip.copy() and pyperclip.paste() functions.
Step 2: Separate the Lines of Text and Add the Star
➢ The call to pyperclip.paste() returns all the text on the clipboard as one big string.
➢ the string stored in text would look like this: 'Lists of animals\nLists of aquarium
life\nLists of biologists by author abbreviation\nLists of cultivars'
➢ The \n newline characters in this string cause it to be displayed with multiple lines
when it is printed or pasted from the clipboard.
➢ There are many “lines” in this one string value.
➢ You want to add a star to the start of each of these lines.
➢ the split() method return a list of strings, one for each line in the original string, and
then add the star to the front of each string in the list.
text = 'Lists of animals\nLists of aquarium life\nLists of biologists by author
abbreviation\nLists of cultivars'
print(text.split('\n'))
output:
['Lists of animals', 'Lists of aquarium life', 'Lists of biologists by
author abbreviation', 'Lists of cultivars']
➢ We split the text along its newlines to get a list in which each item is one line of the
text.
➢ We store the list in lines and then loop through the items in lines.
➢ For each line, we add a star and a space to the start of the line.
➢ Now each string in lines begins with a star
Step 3: Join the Modified Lines
➢ The lines list now contains modified lines that start with stars.
➢ But pyperclip.copy() is expecting a single string value, not a list of string values.
➢ To make this single string value, pass lines into the join() method to get a single
string
joined from the list’s strings.
When this program is run, it replaces the text on the clipboard with text that has stars
at the
start of each line.
➢ Now the program is complete, and you can try running it with text copied to the
26
clipboard.
➢ Even if you don’t need to automate this specific task, you might want to automate
some other kind of text manipulation, such as removing trailing spaces from the end
of lines or converting text to uppercase or lowercase.
➢ Whatever your needs, you can use the clipboard for input and output.
27
Reading and Writing Files
Files and File Paths
➢ A file has two key properties: a filename (usually written as one word) and a
path.
“G:\\B-Tech\\1st Sem\\Introduction to Python Programming\\Module
1\\module1.pdf”
➢ The C:\ part of the path is the root folder, which contains all other folders.
28
➢ On Windows, the root folder is named C:\ and is also called the C: drive.
➢ On OS X and Linux, the root folder is /.
➢ Additional volumes, such as a DVD drive or USB thumb drive, will appear
differently on different operating systems.
➢ On Windows, they appear as new, lettered root drives, such as D:\ or E:\.
➢ On OS X, they appear as new folders under the /Volumes folder.
➢ On Linux, they appear as new folders under the /mnt (“mount”) folder.
➢ Also note that while folder names and filenames are not case sensitive on
Windows and OS X, they are case sensitive on Linux.
➢ On Windows, paths are written using backslashes (\) as the separator between
folder names.
➢ OS X and Linux, however, use the forward slash (/) as their path separator.
os.path.join():
➢ If you want your programs to work on all operating systems, you will have to
write your Python scripts to handle both cases.
➢ This is simple to do with the os.path.join() function.
➢ If you pass it the string values of individual file and folder names in your path,
os.path.join() will return a string with a file path using the correct path
separators.
Enter the following into the interactive shell:
29
➢ In Windows os.path .join('usr', 'bin', 'spam') will return 'usr\\bin\\spam'.
(Notice that the backslashes are doubled because each backslash needs to be
escaped by another backslash character.)
➢ In OS X or Linux, the string would have been 'usr/bin/spam'.
➢ The os.path.join() function is helpful if you need to create strings for filenames.
➢ These strings will be passed to several of the file-related functions.
For example, the following example joins names from a list of filenames to the end of a
folder’s name:
Ex:
>>> myFiles = ['accounts.txt', 'details.csv', 'invite.docx']
>>> for filename in myFiles:
print(os.path.join('C:\\Users\\asweigart', filename))
Output:
C:\Users\asweigart\accounts.txt
C:\Users\asweigart\details.csv
C:\Users\asweigart\invite.docx
30
Enter the following into the interactive shell:
When the current working directory is set to C:\bacon, the relative paths for
the other folders and files are set as they are in the figure.
32
Creating New Folders with os.makedirs()
A program can create new folders (directories) with the os.makedirs() function.
➢ This will create not just the C:\delicious folder but also a walnut
folder inside C:\delicious and a waffles folder inside
C:\delicious\walnut.
➢ That is, os.makedirs() will create any necessary intermediate folders
in order to ensure that the full path exists.
33
The os.path Module
➢ The os.path module contains many helpful functions related to
filenames and file paths.
➢ For instance, os.path.join() will build paths in a way that will work on
any operating system.
➢ Since os.path is a module inside the os module, you can import it by
simply running import os.
34
➢ Calling os.path.isabs(path) (path is always string value) will return
True if the argument is an absolute path and False if it is a relative
path.
Eg:
import os
x = os.path.isabs('file1.txt')
print(x)
output:
False
x = os.path.isabs('C:\\Users\\HP\\file1.txt')
print(x)
output:
True
Eg:
import os
y = os.path.relpath('C:\\Users\\HP\\file1.txt')
print(y)
output:
file1.txt
35
Try these functions in the interactive shell:
Since C:\Python34 was the working directory when os.path.abspath() was called, the
“single-dot” folder represents the absolute path 'C:\\Python34'.
# importing os module
import os
# Path
path = "/home / User / Desktop / file.txt"
36
# to the given path from the
# the given start directory.
print(relative_path)
OUTPUT:
Desktop/file.txt
# Path
path = "/home / User / Desktop / file.txt"
Output:
../../../User/Desktop/file.txt
37
Calling os.path.dirname(path) will return a string of everything that comes before the
last slash in the path argument.
Eg:
os.path.dirname('C:\\Users\\HP\\file1.txt')
output:
'C:\\Users\\HP'
Calling os.path.basename(path) will return a string of everything that comes after the
last slash in the path argument.
Eg:
os.path.baseame('C:\\Users\\HP\\file1.txt')
output:
'file1.txt'
The dir name and base name of a path are outlined in Figure 8-4.
38
39
Finding File Sizes and Folder Contents
Once you have ways of handling file paths, you can then start gathering
information about specific files and folders.
The os.path module provides functions for finding the size of a file in bytes and the
files and folders inside a given folder.
Calling os.path.getsize(path) will return the size in bytes of the file in the path
argument.
Eg:
import os
os.path.getsize('C:\\Users\\HP\\file1.txt')
Output:
106
Calling os.listdir(path) will return a list of filename strings for each file in the path
argument.
Note that this function is in the os module, not os.path.)
Eg:
import os
os.listdir('C:\\Users\\HP\\Hithesh')
Output
40
Checking Path Validity
Many Python functions will crash with an error if you supply them with a path that
does not exist.
The os.path module provides functions to check whether a given path exists and
whether it is a file or folder.
Calling os.path.exists(path) will return True if the file or folder referred to in the
argument exists and will return False if it does not exist.
Eg: os.path.exists('C:\\Users\\HP\\file1.txt')
True
Calling os.path.isfile(path) will return True if the path argument exists and is a file and
will return False otherwise.
Eg:
os.path.isfile('C:\\Users\\HP\\file1.txt')
True
os.path.isfile('C:\\Users\\HP')
False
Calling os.path.isdir(path) will return True if the path argument exists and is a folder
and will return False otherwise.
Eg:
os.path.isdir('C:\\Users\\HP\\file1.txt')
False
os.path.isdir('C:\\Users\\HP')
True
41
The File Reading/Writing Process
➢ Once you are comfortable working with folders and relative paths, you’ll be able
to specify the location of files to read and write.
➢ Plaintext files contain only basic text characters and do not include font, size, or
color information.
➢ Text files with the .txt extension or Python script files with the .py extension are
examples of plaintext files.
➢ These can be opened with Windows’s Notepad or OS X’s TextEdit application.
➢ Programs can easily read the contents of plaintext files and treat them as an
ordinary string value.
➢ Binary files are all other file types, such as word processing documents, PDFs,
images, spreadsheets, and executable programs.
➢ If you open a binary file in Notepad or TextEdit, it will look like scrambled
nonsense, like in Figure 8-5.
42
➢ Since every different type of binary file must be handled in its own way, this
book will not go into reading and writing raw binary files directly.
➢ Fortunately, many modules make working with binary files easier
43
Example:
➢ create a text file named hello.txt using Notepad or TextEdit
➢ Type Hello world! as the content of this text file and save it in your user home
folder.
Enter the following into the interactive shell:
helloFile = open('C:\\Users\\Newfolder\\hello.txt')
Syntax:
file.read(size)
44
size Optional. The number of bytes to return. Default -1, which means the whole file.
Example:
f = open("demofile.txt", "r") Demofile.txt
Hello! Welcome to demofile.txt
print(f.read(33)) This file is for testing purposes.
'Good Luck!'
Output:
Hello! Welcome to demofile.txt
readline() :
The readline() method returns one line from the file.
It can also specify how many bytes from the line to return, by using the size parameter
Syntax:
file.readline(size)
size Optional. The number of bytes from the line to return. Default -1, which
means the whole line
Example:
45
f = open("demofile.txt", "r")
Demofile.txt
print(f.readline())
Hello! Welcome to demofile.txt
This file is for testing purposes.
Output:
'Good Luck!'
Hello! Welcome to demofile.txt
f = open("demofile.txt", "r")
print(f.readline(5))
print(f.readline())
Output:
Hello
This file is for testing purposes.
readlines() :
The readlines() method returns a list containing each line in the file as a list item.
Use the size parameter to limit the number of lines returned.
If the total number of bytes returned exceeds the specified number, no more lines are
returned.
Syntax:
file.readlines(size)
size Optional. If the number of bytes returned exceed the size number, no more lines will be
Default value is -1, which means all lines will be returned.
Example:
Return all lines in the file, as a list where each line is an item in the list object:
46
f = open("demofile.txt", "r")
print(f.readlines())
output:
['Hello! Welcome to demofile.txt\n', 'This file is for testing purposes.\n',
'Good Luck!']
Example
Do not return the next line if the total number of returned bytes are more
than 33:
f = open("demofile.txt", "r")
print(f.readlines(33))
Output:
['Hello! Welcome to demofile.txt\n', 'This file is for testing purposes.\n']
47
Note that each of the string values ends with a newline character, \n , except for the
last line of the file. A list of strings is often easier to work with than a single large
string value.
Writing to Files
The write() method writes a specified text to the file.
Where the specified text will be inserted depends on the file mode.
48
Mode "a"(append):
This mode allows you to open a file for appending new content.
If the file already exists, the new content will be added to the end of the file.
If the file does not exist, it will create a new file.
Syntax:
file.write(text)
Example:
f = open("demofile2.txt", "a")
f.write("See you soon!") Demofile2.txt
f.close() Hello! Welcome to
demofile.txt
#open and read the file after the This file is for testing
appending: purposes.
f = open("demofile2.txt") 'Good Luck!'
print(f.read())
Output:
Hello! Welcome to demofile2.txt
This file is for testing purposes.
Good Luck!See you soon!
49
f = open("demofile2.txt", "w")
f.write("See you soon!") Demofile2.txt
f.close() Hello! Welcome to
demofile.txt
#open and read the file: This file is for testing
f = open("demofile2.txt", "r") purposes.
print(f.read()) 'Good Luck!'
Output:
See you soon!
50
Note that the write() method does not automatically add a newline character to the
end of the string like the print() function does. You will have to add this character
yourself.
51
Saving Variables with the shelve Module
➢ You can save variables in your Python programs to binary shelf files using the
shelve module.
➢ This way, your program can restore data to variables from the hard drive.
➢ `The shelve module will let you add Save and Open features to your program.
➢ For example, if you ran a program and entered some configuration settings, you
could save those settings to a shelf file and then have the program load them
the next time it is run.
➢ To read and write data using the shelve module, you first import shelve.
➢ Call shelve.open() and pass it a filename, and then store the returned shelf value
in a variable.
➢ You can make changes to the shelf value as if it were a dictionary.
➢ When you’re done, call close() on the shelf value.
➢ Here, our shelf value is stored in shelfFile.
➢ We create a list cats and write shelfFile['cats'] = cats to store the list in shelfFile
as a value associated with the key 'cats'(like in a dictionary).
➢ Then we call close() on shelfFile.
52
➢ After running the previous code on Windows, you will see three new files in the
current working directory: mydata.bak, mydata.dat, and mydata.dir.
➢ On OS X, only a single mydata.db file will be created.
➢ These binary files contain the data you stored in your shelf.
➢ The module frees you from worrying about how to store your program’s data to
a file.
➢ These programs can use the shelve module to later reopen and retrieve the data
from these shelf files.
➢ Shelf values don’t have to be opened in read or write mode—they can do both
once opened.
➢ Here, we open the shelf files to check that our data was stored correctly.
➢ Entering shelfFile['cats'] returns the same list that we stored earlier, so we know
that the list is correctly stored, and we call close().
➢ Just like dictionaries, shelf values have keys() and values() methods that will
return list-like values of the keys and values in the shelf.
➢ Since these methods return list-like values instead of true lists, you should pass
them to the list() function to get them in list form.
53
Enter the following into the interactive shell:
54
➢ Here, we import pprint to use pprint.pformat().
➢ We have a list of dictionaries, stored in a variable cats.
➢ To keep the list in cats available even after we close the shell, we use
pprint.pformat() to return it as a string.
➢ Once we have the data in cats as a string, it’s easy to write the string to a file,
which we’ll call myCats.py.
➢ The modules that an import statement imports are themselves just Python
scripts.
➢ When the string from pprint.pformat() is saved to a .py file, the file is a module
that can be imported just like any other.
➢ And since Python scripts are themselves just text files with the .py file extension,
➢ Python programs can even generate other Python programs.
➢ You can then import these files into scripts
55
Shelve Module: Introduction
Shelve Module is an effective tool through which we can store all the data and
information inside a file and keep it in our computer's storage until we change our
mind to remove it.
In the Shelve Module, a shelf object is defined, which acts like a dictionary-type
object, and it is stored in the disk file of the computer.
Shelve Module is not only helpful in storing information inside a file, but it is also very
effective in modifying the already present information and adding some new
information in the same file.
We can perform all these operations (creating, reading, writing, and deleting from a file)
from the Shelve Module and its functions inside a Python program.
The Shelve Module creates a very similar file to the DBMS database present on systems
such as UNIX.
We can only use string data type as the keys in the special dictionary object present in
the file.
56