Module3 Strings and File handling-notesshort
Module3 Strings and File handling-notesshort
Prepared by: Dr. B Latha Shankar, Associate Professor, IEM Department, SIT Tumakuru
Syllabus: 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: Multi-clipboard.
Textbook 1: Chapters 6, 8 08 hrs.
How are string literals in Python defined and utilized within the language?
String values in Python begin and end with a single /double /triple quotes
Benefit of using double/triple quotes: string can have a single quote character in it.
>>> spam = "That is Alice's cat“
>>> print(spam)
O/P: That is Alice's cat
What to do, if need to use both single quotes and double quotes in the string???
Use escape characters.
What are escape characters in Python, and how do they function to modify the
interpretation of certain characters within strings? Give illustrative examples.
An escape character lets to 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. Ex: the escape character for a single quote is \'.
Use this inside a string that begins and ends with single quotes:
>>> spam = 'Say hi to Bob\'s mother.‘
O/P: Say hi to Bob's mother.
>>> print("Hello there!\n How are you?\n I\'m doing fine.")
O/P:
Hello there!
How are you?
I'm doing fine.
Table 3.1 List of escape characters
3-1
Module-3: Manipulating Strings, Reading and Writing Files
How do raw strings differ from regular strings in terms of syntax and functionality?
Explain.
A raw string completely ignores all escape characters and prints any backslash that appears in the
string. Place an r before the beginning quotation mark of a string to make it a raw string:
>>> print(r ' That is Carol\'s cat.')
O/P: That is Carol\'s cat.
Raw strings are helpful while typing string values that contain many backslashes, such as the strings
used for Windows file paths like:
>>> r'C:\Users\Al\Desktop'
O/P: 'C:\\Users\\Al\\Desktop'
O/p:
Dear Alice,
Eve's cat has been arrested for catnapping, cat burglary, and extortion.
Sincerely,
Bob
How does string indexing and string slicing work in programming, and what are the key
differences between them when manipulating strings?
1. Indexing: the process of accessing a specific element in a sequence (ex: string or list) using its position
called as index.
3-2
Module-3: Manipulating Strings, Reading and Writing Files
Consider the string 'Hello, world!' as a list and each character in the string as an item with a
corresponding index.
The space and exclamation point are included in the character count.
H e l l o , w o r l d !
0 1 2 3 4 5 6 7 8 9 10 11 12
Ex:
>>> spam = 'Hello, world!'
>>> spam[0]
O/p: 'H'
If an index has a negative value, it counts backward from the end of the string:
>>> spam[-1]
O/p: '!'
H e l l o , w o r l d !
0 1 2 3 4 5 6 7 8 9 10 11 12
-13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
2. Slicing: It enables users to access the specific range of elements of a sequence (Ex: string, list) by
mentioning the indices.
Syntax: String[start:stop:step] “Start” specifies the starting index of a slice. “Stop” specifies the
ending element of a slice. If you specify a range from one index to another, the starting index is
included and the ending index is not:
>>> spam[0:5]
O/p: 'Hello'
If you omit the first index (before the colon), the slice starts at the beginning of the string. If you
omit the second index, the slice goes to till the end of the string:
>>> spam[:5] # first index is omitted and hence string starts from beginning
O/p: 'Hello'
>>> spam[7:] #Last index is omitted and hence slice goes till the end of string
O/p: 'world!'
If you omit both the indices, the slice is a copy of the whole string.
>>> spam[:] # Both the indices omitted and hence the slice is a copy of string
O/p: ‘Hello, world!’
Note that slicing a string does not modify the original string, because string is immutable.
How the 'in' and 'not in' operators are used to check for membership in strings in Python?
in or not in will evaluate to a Boolean True or False.
These expressions test whether the first string (case sensitive) can be found within the second
string.
Ex:
>>> 'Hello' in 'Hello, World'
O/P: True
>>> spam1 = ['hello', 'hi', 'howdy', 'heyas']
>>> 'cat' in spam1
O/P: False
>>> 'howdy' not in spam1
O/P: False
Syntax:
>>> ELEMENTNAME in LISTNAME
>>> ELEMENTNAME not in LISTNAME
How can one effectively embed or concatenate strings within other strings in a
3-3
Module-3: Manipulating Strings, Reading and Writing Files
programming context?
Strings can be put inside other strings(=obtaining a new string that contains all of the original
strings) using:
1. string concatenation using + operator
Ex:
>>> name = 'Al'
>>> age = 4000
>>> 'Hello, my name is ' + name + '. I am ' + str(age) + ' years old.'
O/p: 'Hello, my name is Al. I am 4000 years old.'
2. String interpolation: %s operator inside the string acts as a marker to be replaced by values
following the string. Benefit: Need not use str() to convert values into string.
Ex:
>>> name = 'Al'
>>> age = 4000
>>> 'My name is %s. I am %s years old.' % (name, age)
O/p: 'My name is Al. I am 4000 years old.'
3. f-string: which is similar to string interpolation except that braces are used instead of %s, with the
expressions placed directly inside the braces. f-strings have an f prefix before the starting quotation
mark:
Ex:
>>> name = 'Al'
>>> age = 4000
>>> f 'My name is {name}. Next year I will be {age + 1}.'
O/p: 'My name is Al. Next year I will be 4001.'
O/p: True
>>> '12345'.islower()
O/p: False
The string methods can be called on the strings returned by upper () and lower () string
methods and called as a chain of method calls.
>>> 'Hello'.upper().lower()
O/p: 'hello'
>>> 'Hello'.upper().lower().upper()
O/p: 'HELLO'
>>> 'HELLO'.lower().islower()
O/p: True
Develop a program that repeatedly asks users for their age and a password until they
provide valid input.
while True:
print('Enter your age:')
age = input()
if age.isdecimal():
break
print('Please enter a number for your age.')
while True:
print('Select a new password (letters and numbers only):')
password = input()
if password.isalnum():
break
print('Passwords can only have letters and numbers.')
3-5
Module-3: Manipulating Strings, Reading and Writing Files
O/P:
Enter your age:
forty two
Please enter a number for your age.
Enter your age:
42
Select a new password (letters and numbers only):
secr3t!
Passwords can only have letters and numbers.
Select a new password (letters and numbers only):
secr3t
Ex:
>>> 'Hello, world!‘ . startswith('Hello')
O/p: True
>>> 'Hello, world!'.endswith('world!')
O/p: True
Distinguish between the join() and split() Methods with illustrative examples.
join() method:
join() method returns a single string got from concatenation of all strings present in the passed
list.
Use: when a list of strings are to be joined together into a single string.
The join() method is
called on a string,
a list of strings is passed,
returns single string.
join() method calls on a string, which will be inserted between each string of list argument during
concatenation.
Ex:
>>> ', '. join(['cats', 'rats', 'bats']) #’called on string’ is a comma, so, o/p has comma as separator
O/p: 'cats, rats, bats'
>>> ' ‘ . join(['My', 'name', 'is', 'Simon']) #’called on string’ is white space, so, o/p has space as separator
O/p: 'My name is Simon'
>>> 'ABC'.join(['My', 'name', 'is', 'Simon']) #’called on string’ is ‘ABC’, so, o/p has ABC as separator
O/p: 'MyABCnameABCisABCSimon'
3-6
Module-3: Manipulating Strings, Reading and Writing Files
split() method:
The split() method does the opposite, compared to join() method: It takes in a single string
and returns a list of strings.
By default, the string 'My name is Simon' is split wherever whitespace characters (= space, tab, or
newline characters) are found.
These whitespace characters are not included in strings in the returned list.
Ex:
>>> 'My name is Simon'.split()
O/p: ['My', 'name', 'is', 'Simon']
Pass a ‘delimiter string’ to the split() method to split upon, other than at white spaces.
This passed string should be present in called on string.
Ex:
>>>'MyABCnameABCisABCSimon'.split('ABC')
O/p: ['My', 'name', 'is', 'Simon']
>>> 'My name is Simon'.split('m')
O/p: ['My na', 'e is Si', 'on']
If the separator string passed to partition(), occurs multiple times in the ‘called on’ string, the
method splits the string only on the first occurrence:
>>> 'Hello, world!'. partition('o')
O/p: ('Hell', 'o', ', world!')
If the separator string can’t be found, the first string returned in the tuple will be the entire string,
and the other two strings will be empty:
>>> 'Hello, world!‘ . partition('XYZ')
O/p: ('Hello, world!', ‘ ', ‘ ')
Use the ‘multiple assignment trick’ to assign the three returned strings to three variables:
>>> before, sep, after = 'Hello, world!'.partition(' ')
>>> before
3-7
Module-3: Manipulating Strings, Reading and Writing Files
O/p: 'Hello,'
>>> after
O/p: 'world!'
Use: for splitting a string into the parts before and after a particular separator string.
Discuss few string methods for Justifying Text and Removing Whitespace:
rjust() and ljust() string methods justifies the string they are called on, with spaces inserted
to justify the text.
First argument to both methods is an integer length for the justified string.
Ex:
>>> 'Hello‘ . rjust(10) # right-justifies 'Hello' in a string of total length 10.
O/p: ‘ Hello' # 'Hello' is of 5 characters, so five spaces will be added to
#its left, with 'Hello' justified right.
>>> 'Hello'.ljust(15) # left-justifies 'Hello' in a string of total length 15.
O/p: 'Hello ‘
An optional second argument to rjust() and ljust() will specify a fill character other than a
space character:
>>> 'Hello‘.rjust(20, '*')
O/p: '***************Hello'
The center() string method works like ljust() and rjust() but centers the text rather than
justifying it to the left or right:
>>> 'Hello'.center(20, '=')
O/p: '=======Hello========'
Use of these methods: to print tabular data in a neatly aligned format with correct spacing.
Demonstrate justifying mathods using a snippet
def printPicnic(itemsDict, leftWidth, rightWidth): #function definition
print('PICNICITEMS'.center(leftWidth+rightWidth, '-')) #prints at center
for k,v in itemsDict.items(): #extracts keys and values from dict.
print(k.ljust(leftWidth, '.')+str(v).rjust(rightWidth))#prints keys left
#justified and values right justified
-------PICNICITEMS--------
sandwitches......... 4
apples.............. 12
cups................ 4
cookies............. 8000
3-8
Module-3: Manipulating Strings, Reading and Writing Files
strip(), rstrip() and lstrip() methods are used to cut off whitespace characters (space,
tab, and newline). strip()removes from the both sides, rstrip() removes from right side
and lstrip() removes from left side of a string.
Ex:
>>> spam = ' Hello, World '
>>> spam . strip() # will remove whitespace characters from both the ends
O/p: 'Hello, World'
>>> spam.lstrip() # will remove whitespace characters from left end.
O/p: 'Hello, World '
>>> spam.rstrip() # will remove whitespace characters from right end.
O/p: ' Hello, World'
An optional second argument to above methods will specify which characters on the ends should
be stripped:
>>> spam = 'SpamSpamBaconSpamEggsSpamSpam'
>>> spam.strip('ampS') # strip occurrences of a, m, p, and capital S from the ends of
the string stored in spam. The order of the characters does
not matter: strip('ampS') will do the same thing as
strip('mapS') or strip('Spam').
O/p: 'BaconSpamEggs'
Use chr() function to get the one-character string of an integer code point:
>>> ord(65)
O/p: ‘A’
3-9
Module-3: Manipulating Strings, Reading and Writing Files
Ex:
>>> import pyperclip
>>> pyperclip.copy('Hello, world!')
>>> pyperclip.paste()
O/p: 'Hello, world!'
Use Path() function in the pathlib module. Path() function returns Path object of strings passed
correct to the operating system used.
Following code joins filenames to the end of a folder’s name, one at a time:
>>> from pathlib import Path
>>> myFiles = ['accounts.txt', 'details.csv', 'invite.docx']
>>> for filename in myFiles:
3-10
Module-3: Manipulating Strings, Reading and Writing Files
O/p: C:\Users\Al\accounts.txt
O/p: C:\Users\Al\details.csv
O/p: C:\Users\Al\invite.docx
The pathlib module uses the ‘/’ operator to join paths correctly, no matter, on what operating
system code is running.
>>> homeFolder = Path('C:/Users/Al')
>>> subFolder = Path('spam')
>>> homeFolder / subFolder
O/p: WindowsPath('C:/Users/Al/spam')
>>> str(homeFolder / subFolder)
O/p: 'C:\\Users\\Al\\spam'
Condition: when using the ‘/’ operator for joining paths then, one of the first two values must be
a Path object. Else Python will give an error:
>>> 'spam' / 'bacon' / 'eggs' # one of the first two values is not a Path object
O/p: Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'str' and 'str'
Python will display an error if you try to change to a directory that does not exist.
>>> os.chdir('C:/ThisFolderDoesNotExist')
O/p: Traceback (most recent call last):
File "<stdin>", line 1, in <module>
3-11
Module-3: Manipulating Strings, Reading and Writing Files
What is the distinction between absolute and relative paths in the context of file systems,
and how do they differ in specifying file or directory locations within a computer system?
There are two ways to specify a file path:
An absolute path, which always begins with the root folder
A relative path, which is relative to program’s cwd.
A single period . (“dot”) =“this directory.” (CWD)
Two periods .. (“dot dot”) =“the parent folder(directory).”
When cwd is set to C:\bacon, relative paths for other folders & files are set as : (The
.\ at start of a relative path is optional)
Absolute Path Relative Path
C:\ .. Or ..\
C:\ bacon . Or .\
C:\bacon\fizz . \ fizz
C:\ bacon\ fizz\spam.txt . \ fizz\spam.txt
C:\ bacon\spam.txt . \ spam.txt
C:\ eggs .. \ eggs
C:\ eggs \ spam.txt .. \ eggs\spam.txt
3-12
Module-3: Manipulating Strings, Reading and Writing Files
is_absolute() method: Will return True if it represents an absolute path and False if it represents
a relative path when called on a Path object
Ex:
>>> Path.cwd()
O/P: WindowsPath('C:/Users/Al/Python37')
>>> Path.cwd().is_absolute()
O/P: True
>>> Path('spam/bacon/eggs') . is_absolute()
O/P: False
Two Options are available: To get an absolute path from a relative path:
1.add Path.cwd() / in front of the relative Path object:
>>> Consider relative path: 'my/relative/path')
>>> Path.cwd() / Path('my/relative/path')
O/P: WindowsPath('C:/Users/Al/AppData/Local/Programs/Python/Python37/my/relative/path')
3-13
Module-3: Manipulating Strings, Reading and Writing Files
O/P: 'C:\\'
>>> p.parent # This is a Path object, not a string.
O/P: WindowsPath('C:/Users/Al')
>>> p.name
O/P: 'spam.txt'
>>> p.stem
O/P: 'spam'
>>> p.suffix
O/P: '.txt'
>>> p.drive
O/P: 'C:'
>>> Path.cwd()
O/P: WindowsPath('C:/Users/Al/AppData/Local/Programs/Python/Python37')
>>> Path.cwd().parents[0]
O/P: WindowsPath('C:/Users/Al/AppData/Local/Programs/Python')
>>> Path.cwd().parents[1]
O/P: WindowsPath('C:/Users/Al/AppData/Local/Programs')
>>> Path.cwd().parents[2]
O/P: WindowsPath('C:/Users/Al/AppData/Local')
>>> Path.cwd().parents[3]
O/P: WindowsPath('C:/Users/Al/AppData')
>>> Path.cwd().parents[4]
O/P: WindowsPath('C:/Users/Al')
>>> Path.cwd().parents[5]
O/P: WindowsPath('C:/Users')
>>> Path.cwd().parents[6]
O/P: WindowsPath('C:/')
• os.path.split() : To get a tuple of path’s dir name and base name together:
>>> calcFilePath = 'C:\\Windows\\System32\\calc.exe‘
>>> os.path.split(calcFilePath)
O/P: ('C:\\Windows\\System32', 'calc.exe')
Get the same tuple using: os.path.dirname() and os.path.basename() and place their return
values in a tuple:
>>> (os.path.dirname(calcFilePath), os.path.basename(calcFilePath))
O/P: ('C:\\Windows\\System32', 'calc.exe')
Use the split() string method and split at os.sep: To get a list of strings of each folder.
>>> calcFilePath.split(os.sep)
#returns all the parts of the path as strings
#separately
O/P: ['C:', 'Windows', 'System32', 'calc.exe']
3-14
Module-3: Manipulating Strings, Reading and Writing Files
How do you find the File Sizes and Folder Contents in Python?
The os.path module provides getsize () function for finding the size of a file in bytes and
listdir() functions for listing the files and folders inside a given folder.
Ex:
>>> os.path.getsize('C:\\Windows\\System32\\calc.exe')
O/P: 27648
>>> os.listdir('C:\\Windows\\System32')
O/P: ['0409', '12520437.cpx', '12520850.cpx', '5U877.ax', 'aaclient.dll',
--snip--
'xwtpdui.dll', 'xwtpw32.dll', 'zh-CN', 'zh-HK', 'zh-TW', 'zipfldr.dll']
How to find the total size of all the files in this directory?
Use os.path.getsize() and os.listdir() together.
Loop over each filename in the C:\Windows\System32 folder, so that the total Size variable is
incremented by the size of each file.
Along with os.path.getsize(), os.path.join() is used to join the folder name with the
current filename.
The integer that os.path.getsize() returns is added to value of totalSize.
Ex. program:
>>> totalSize = 0
>>> for filename in os.listdir('C:\\Windows\\System32'):
totalSize = totalSize +
os.path.getsize(os.path.join('C:\\Windows\\System32', filename))
>>> print(totalSize)
O/P: 2559970473
The question mark ‘?’ stands for any single character. The glob expression 'project?.docx' will
return 'project1.docx' or 'project5.docx', but it will not return 'project10.docx', because
‘?’ only matches to one character—so it will not match to two-character string '10'.
3-15
Module-3: Manipulating Strings, Reading and Writing Files
Ex:
>>> list(p.glob('project?.docx')
O/P: [WindowsPath('C:/Users/Al/Desktop/project1.docx'), WindowsPath('C:/Users/Al/
Desktop/project2.docx'),
--snip--
WindowsPath('C:/Users/Al/Desktop/project9.docx')]
Combine the asterisk and question mark; use the glob expression '*.?x?' to return files with
any name and any three-character extension where the middle character is an 'x'.
Use a for loop to iterate over the generator that glob() returns:
>>> p = Path('C:/Users/Al/Desktop')
>>> for textFilePathObj in p.glob('*.txt'):
... print(textFilePathObj) # Prints the Path object as a string.
O/P: C:\Users\Al\Desktop\foo.txt
C:\Users\Al\Desktop\spam.txt
C:\Users\Al\Desktop\zzz.txt
Use: If you want to perform some operation on every file in a directory, use either os.listdir(p)
or p.glob('*').
How to determine whether there is a DVD or flash drive currently attached to the computer? By
checking for it with the exists() method.
>>> dDrive = Path('D:/')
>>> dDrive.exists()
O/P: False
>>> p = Path('spam.txt')
>>> p.write_text('Hello, world!') #creates spam.txt file with content 'Hello,world!'
O/P: 13 # write_text()indicates that 13 characters were written to file.
>>> p.read_text()
O/P: 'Hello, world!' #reads and returns the contents of new file as a string
readlines() method: returns a list of strings, one string for each line of the file.
Ex:
Create a file named sonnet29.txt in the same directory as hello.txt and write following
text in it:
When, in disgrace with fortune and men's eyes,
I all alone beweep my outcast state,
And trouble deaf heaven with my bootless cries,
And look upon myself and curse my fate.
>>> sonnetFile = open(Path.home() / 'sonnet29.txt')
>>> sonnetFile.readlines() # returns a list of string values, one string for each
#line of the file
O/P: [‘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.']
Except for the last line of the file, each of the string values ends with a newline character ‘\n’.
Writing to Files:
Python allows to write content to a file, similar to how print() function “writes” strings to screen
3-17
Module-3: Manipulating Strings, Reading and Writing Files
To write contents to a file it has to be opened in ‘write mode’ or ‘append mode’, not in ‘read mode’.
Pass 'w' as the second argument to open() to open the file in write mode.
‘Write mode’ will overwrite the existing file and start from scratch.
Ex:
>>> baconFile = open('bacon.txt', 'w')
>>> baconFile.write('Hello, world!\n')
O/P: 14
>>> baconFile.close()
>>> baconFile = open('bacon.txt', 'a')
>>> baconFile.write('Bacon is not a vegetable.')
O/P: 25
>>> baconFile.close()
>>> baconFile = open('bacon.txt')
>>> content = baconFile.read()
>>> baconFile.close()
>>> print(content)
O/P: Hello, world!
Bacon is not a vegetable.
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.
>>> baconFile = open('bacon.txt', 'w')
>>> baconFile.write('Hello, world!\n')
O/p: 14
After running the previous code on Windows, three new files in the current working directory will
be visible: mydata.bak, mydata.dat, and mydata.dir.
How to retrieve data from shelve module?
Use the shelve module to reopen and retrieve the data from saved shelf files.
Shelf values don’t have to be opened in read or write mode—they can do both once opened.
Entering shelfFile['cats'] returns the same list that was stored earlier, and use close() to close
it.
>>> shelfFile = shelve.open('mydata')
>>> type(shelfFile)
O/P: <class 'shelve.DbfilenameShelf'>
>>> shelfFile['cats'] # Returns content of list named ‘cats’
O/P: ['Zophie', 'Pooka', 'Simon']
>>> shelfFile.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.
3-18
Module-3: Manipulating Strings, Reading and Writing Files
Since these methods return list-like values instead of true lists, pass them to the list() function
to get them in list form.
>>> shelfFile = shelve.open('mydata')
>>> list(shelfFile.keys())
O/P: ['cats']
>>> list(shelfFile.values())
O/P: [['Zophie', 'Pooka', 'Simon']]
>>> shelfFile.close()
To keep the list in cats available even after closing the shell, 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, called myCats.py.
The modules are Python scripts.
When the string from pprint.pformat() is saved to a .py file, the file can be imported as a module
just like any other:
>>> import myCats
>>> myCats.cats
O/P: [{'name': 'Zophie', 'desc': 'chubby'}, {'name': 'Pooka', 'desc': 'fluffy'}]
>>> myCats.cats[0]
O/P: {'name': 'Zophie', 'desc': 'chubby'}
>>> myCats.cats[0]['name']
O/P: 'Zophie'
The benefit of creating a .py file (as opposed to saving variables with the shelve module) is
that because it is a text file, the contents of the file can be read and modified by anyone with
a simple text editon
3-19
Module-3: Manipulating Strings, Reading and Writing Files
Step 2: Create the Quiz File and Shuffle the Question Order
# Generate 35 quiz files.
for quizNum in range(35):
quizFile = open(f 'capitalsquiz{quizNum+1}.txt','w') #creates quizfile
#&opens in write mode
answerKeyFile = open(f 'capitalsquiz_answers{quizNum + 1}.txt', 'w')#creates
#quiz answer keyfile &opens in write mode
# Write out the header for the quiz.
quizFile.write('Name:\n\nDate:\n\nPeriod:\n\n') #creates header in quizfile
quizFile.write((' ' * 20) + f 'State Capitals Quiz (Form{quizNum + 1})')
quizFile.write('\n\n')
states = list(capitals.keys()) #creates a list of states from the dictionary
random.shuffle(states) #Shuffles the above list
O/p:
Sample quiz paper: capitalsquiz1.txt :
Name:
Date:
Period:
State Capitals Quiz (Form 1)
1. What is the capital of West Virginia?
A. Hartford
B. Santa Fe
C. Harrisburg
D. Charleston
2. What is the capital of Colorado?
A. Raleigh
B. Harrisburg
C. Denver
D. Lincoln
--snip--
Sample Answer Key File: capitalsquiz_answers1.txt :
1. D
2. C
3. A
4. C
--snip--
Lists of animals
Lists of aquarium life
Lists of biologists by author abbreviation
Lists of cultivars
3-21
Module-3: Manipulating Strings, Reading and Writing Files
# This program adds bullet points to the start of each line of text on the clipboard.
Step 1: Copy and Paste from the Clipboard:
import pyperclip
text = pyperclip.paste() # assigns entire text copied as single string to variable text
Abstract of string.methods and string related functions, file.methods and file related
functions(for quick reference):
Sl. Command Meaning Example
No.
Manipulating Strings:
1. escape character A backslash \ followed \n: new line
by the character you
want to insert.
2. Raw String completely ignores all >>> print(r ' That is Carol\'s cat.')
escape characters and O/P: That is Carol\'s cat.
prints any backslash that
appears in the string
3. multiline string in print Used to put a newline print('''Dear Alice,
statement into a string instead of \n Eve's cat has been arrested''')
escape character
O/p:
Dear Alice,
Eve's cat has been
arrested
4. multiline string used for comments that """This is a multiline comment."""
span multiple lines print('Hello!')
O/p: Hello!
string)
7. in and not in operators tests whether the first >>> spam1 = ['hello', 'howdy']
string (case sensitive) >>> 'cat' in spam1
can be found within the O/P: False
second string. >>> 'cat' not in spam1
O/p: True
8. Joining the strings obtaining a new string >>> name = 'Al'
>>> age = 4000
that contains both of
the original strings:
1. >>> 'Hello, my name is ' + name + '.
I am ' + str(age) + ' years old.'
concatenation:Uses
O/p: 'Hello, my name is Al. I am 4000
‘+’ operator years old.'
2. String >>> 'My name is %s. I am %s years
interpolation: old.' % (name, age)
O/p: 'My name is Al. I am 4000 years
Uses %s operator
old.'
that acts as a marker
to be replaced by
values following the
string
3.f-string: similar >>> f 'My name is {name}. Next year I
will be {age + 1}.'
to string interpolation O/p: 'My name is Al. Next year I will
except that braces are be 4001.'
used instead of %s,
with the expressions
placed directly inside
the braces
String Methods:
9. .upper() Returns all the letters >>> spam = spam.upper()
>>> spam
in the original string to O/p: 'HELLO, WORLD!'
uppercase
10. .lower() Returns all the letters >>> spam = spam.lower()
>>> spam
in the original string to O/p: 'hello, world!'
uppercase
.isX() string methods:
11. .isupper() Returns Boolean True if >>> 'HELLO'.isupper()
string has at least one O/p: True
letter and all the letters
are uppercase else
False.
30.
/ operator combines Path >>> from pathlib import Path
>>> Path('spam') / 'bacon' / 'eggs')
objects and strings O/p: WindowsPath('spam/bacon/eggs')
31. Path.cwd() Gives the cwd as a >>> from pathlib import Path
>>> import os
string >>> Path.cwd()
O/p:
WindowsPath('C:/Users/Python37')
32. Changes the cwd >>> os.chdir('C:\\Windows\\System32')
os.chdir()
>>> Path.cwd()
O/p:WindowsPath('C:/Windows/System32')
33. Path.home() Returns a Path object >>> Path.home()
O/p: WindowsPath('C:/Users/Al')
of the home folder
3-25
Module-3: Manipulating Strings, Reading and Writing Files
36.
Add Path.cwd()to relative Used to get an >>> Path('my/path')
path absolute path from O/P: WindowsPath('my/path')
relative path. >>> Path.cwd() / Path('my/path')
O/P: WindowsPath('C:/Users/Al/my/path')
37.
Add Add this to relative
Path.home()to >>> Path('my/path')
relative path path to get an O/P: WindowsPath('my/path')
absolute path from >>> Path.home() / Path('my/path')
relative path. O/P: WindowsPath('C:/Users/Al/my/path')
38. os.path.abspath(path) Returns a string of >>> os.path.abspath('.')
absolute path of the O/P: 'C:\\Users\\Al\\AppData\\Local
argument \\Programs\\Python\\Python37'
3-26
Module-3: Manipulating Strings, Reading and Writing Files
Glob Patterns
52. .glob() Lists the contents of a
folder according to a
glob pattern
53. The asterisk (*) >>> p = Path('C:/Users/Al/Desktop')
.glob(*)
>>> p.glob('*') 1.
stands for “multiple of O/P: <generator object Path.glob at
any characters”. 0x000002A6E389DED0>
>>> list(p.glob('*'))
glob('*') returns a O/P:
[WindowsPath('C:/Users/Al/Desktop/1.
generator of all files in png'), --snip--
the path stored in WindowsPath('C:/Users/Al/Desktop/zzz
Path. Use .txt')]
list(.glob(*)) to list
all the contents of a
folder.
54. list(p.glob('*.txt') Lists all the files with O/P:
[WindowsPath('C:/Users/Al/Desktop/fo
.txt extension. o.txt'),
--snip--
WindowsPath('C:/Users/Al/Desktop/zzz
.txt')]
55. ‘?’ only matches to >>> list(p.glob('project?.docx')
list(p.glob
O/P:
(' ?.docx')
one character. [WindowsPath('C:/Users/Al/Desktop/pr
oject1.docx'), --snip--
will return WindowsPath('C:/Users/Al/Desktop/pro
'project1.docx' to ject9.docx')]
till 'project5.docx',
but it will not return
'project10.docx'
56. '*.?x?' Returns files with any
name and any three-
character extension
where the middle
character is an 'x'
Checking Path Validity
57.
If p has a Path object: Returns True if path >>> winDir = Path('C:/Windows')
>>> winDir.exists()
exists or returns False O/P: True
p.exists()
if it doesn’t exist
3-27
Module-3: Manipulating Strings, Reading and Writing Files
3-28
Module-3: Manipulating Strings, Reading and Writing Files
3-29