Module 3MYWRTTWYUKM
Module 3MYWRTTWYUKM
ENGINEERING
(Affiliated to Visvesvaraya Technological University, Belagavi & Approved by AICTE, New Delhi.)
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.
>>> print(‘ She said, "Thank you! It's mine “ ') >>> print('She said, \"Thank you! It\'s mine.\“ ')
print('''Dear Alice,
Eve's cat has been arrested for catnapping, cat burglary, and extortion.
Sincerely,
Bob''')
Output:
Dear Alice,
Eve's cat has been arrested for catnapping, cat burglary, and extortion.
Sincerely,
Bob
1 March 2025 Dept of ISE SAPTHAGIRI COLLEGE OF ENGINEERING 6
Without using multiline Strings with triple quotes
print('Dear Alice,\n\nEve\'s cat has been arrested for catnapping, cat burglary, and extortion.\n\nSincerely,\nBob')
Output:
Dear Alice,
Eve's cat has been arrested for catnapping, cat burglary, and extortion.
Sincerely,
Bob
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
s = "-"
s = s.join(list1)
print(s)
1-2-3-4
Notice that the string join() calls on is inserted between each string of the list argument.
note
Pyperclip does not come with
Python. To install it, follow the
directions for installing third-party
modules in Appendix A. After
installing the pyperclip module,
C:\Users\asweigart\Documents\project.docx
path
fileName
Output:
C:\Users\asweigart\accounts.txt
C:\Users\asweigart\details.csv
C:\Users\asweigart\invite.docx
import os
import os
os.chdir('C:\\Windows\\System32')
print(os.getcwd()) print(os.getcwd())
Output: Output:
C:\Users\Admin\.spyder-py3 C:\Windows\System32
There are also the dot (.) and dot-dot (..) folders. These are not real folders but special
names that can be used in a path.
A single period (“dot”) for a folder name is shorthand for “this directory ”
Two periods (“dot-dot”) means “the parent folder ”
import os
os.makedirs('C:\\delicious\\walnut\\waffles')
Calling os.path.isabs(path) will return True if the argument is an absolute path and
False if it is a relative path.
import os
import os
print( os.path.isabs('C:\\Users\\Admin\\.spyder-py3'))
print( os.path.isabs('.'))
Output:
output
True
False
1 March 2025 Dept of ISE SAPTHAGIRI COLLEGE OF ENGINEERING 39
Enter the following calls to os path relpath() into the interactive shell:
Calling os.path.relpath(path, start) will return a string of a relative path from the start
path to path. If start is not provided, the current working directory is used as the start
path.
import os
print(os.path.relpath('C:\\Windows', 'C:\\'))
Output:
Windows
Notice that you could create the same tuple by calling os.path.dirname() and
os.path.basename() and placing their return values in a tuple.
• 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.
• Calling os path isfile(path) will return True if the path argument exists and is a file
and will return False otherwise.
• Calling os path isdir(path) will return True if the path argument exists and is a folder
and will return False otherwise.
Try it by creating 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.
output
output
["When, in disgrace with fortune and men's eyes,\n", 'I all
alone beweep my outcast state,\n', 'And trouble deaf
heaven with my bootless cries,\n', 'And look upon myself
and curse my fate,']
Write mode:
Write mode will overwrite the existing file and start from scratch, just like when you
overwrite a variable’s value with a new value. Pass 'w' as the second argument to
open() to open the file in write mode
readname = open('C:\\Users\\TEST\\bacon.txt','r')
helloread = readname.read()
print(helloread)
output
MY NAME IS SUDARSANAN
output
WHAT IS YOUR NAME
Output:
• Only string data type can be used as key in this special dictionary object
• After running the code on Windows, you will see three new files in the
current working directory: mydata bak, mydata dat, and mydata dir
output
23
25
shelfFile = shelve.open('mydata')
print(shelfFile[‘cats'])
shelfFile.close()
Recall from “Pretty Printing” that the pprint.pprint() function will “pretty print” the contents of a list or dictionary to
the screen
import pprint
cats = [{'name': 'Zophie', 'desc': 'chubby'}, {'name': 'Pooka', 'desc': 'fluffy'}]
pprint.pformat(cats)
fileObj = open('myCats.py', 'w')
fileObj.write('cats = ' + pprint.pformat(cats) + '\n')
fileObj.close()
cats = [{'desc': 'chubby', 'name': 'Zophie'}, {'desc': 'fluffy', 'name': 'Pooka'}] myCats.py
Output:
Zophie
1 March 2025 Dept of ISE SAPTHAGIRI COLLEGE OF ENGINEERING 72
Project: Generating Random Quiz Files
Say you’re a geography teacher with 35 students in your class and you want to give a
pop quiz on US state capitals.
states
['Maryland', 'Michigan', 'Alabama', 'Wisconsin', 'Vermont', 'North Carolina', 'Nebraska', 'Rhode Island', 'Colorado', 'Texas',
'Oregon', 'Tennessee', 'Delaware', 'North Dakota', 'Idaho', 'New Mexico', 'Mississippi', 'Ohio', 'New Jersey', 'Connecticut', 'West
Virginia', 'Illinois', 'California', 'South Dakota', 'Florida', 'Arizona', 'Maine', 'Missouri', 'Alaska', 'New York', 'Minnesota',
'Georgia', 'Montana', 'Louisiana', 'New Hampshire', 'Hawaii', 'South Carolina', 'Indiana', 'Virginia', 'Utah', 'Wyoming',
'Massachusetts', 'Arkansas', 'Nevada', 'Pennsylvania', 'Oklahoma', 'Kentucky', 'Iowa', 'Washington', 'Kansas']
['Arizona', 'Kansas', 'West Virginia', 'South Carolina', 'Wisconsin', 'Alabama', 'Connecticut', 'Washington', 'Maine', 'Wyoming',
'New York', 'Alaska', 'New Hampshire', 'Missouri', 'Minnesota', 'Arkansas', 'Maryland', 'Oklahoma', 'Illinois', 'New Mexico', 'New
Jersey', 'North Carolina', 'Colorado', 'Indiana', 'Ohio', 'Mississippi', 'Idaho', 'Montana', 'Nebraska', 'Kentucky', 'Utah', 'Oregon',
'California', 'Iowa', 'Florida', 'Michigan', 'Nevada', 'Georgia', 'Pennsylvania', 'North Dakota', 'Louisiana', 'Massachusetts',
'Tennessee', 'Hawaii', 'Texas', 'Vermont', 'South Dakota', 'Delaware', 'Virginia', 'Rhode Island']
s v. Copying and pasting will require the pyperclip module, and reading the command line arguments will require the sys
module The shelve module will also come in handy: Whenever the user wants to save a new piece of clipboard text, you’ll
save it to a shelf file. Then, when the user wants to paste the text back to their clipboard, you’ll open the shelf file and load it
back into your program. The shelf file will be named with the prefix mcb w.