Module 3 Notes
Module 3 Notes
Python
Programming
MAY 20
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 [Link] Module, The File
Reading/Writing.
Process, Saving Variables with the shelve Module, Saving Variables with the
[Link]() 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 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.
Enter the following into the interactive shell:
3
>>> spam = 'Say hi to Bob\'s mother.'
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.
4
➢ 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
The upper()
lower()
isupper() and
islower()
The upper()
➢ The upper() method returns a string where all characters are in upper case.
➢ Symbols and Numbers are ignored.
Syntax: [Link]()
8
Parameters: The upper() method doesn’t take any parameters.
Returns: returns an uppercased string of the given string.
Ex:
>>> spam = 'Hello world!'
>>> spam = [Link]()
>>> 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 = [Link]() to change the string in spam
instead of simply [Link]().
➢ (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.
The lower()
Python String lower() method converts all uppercase characters in a string into
lowercase characters and returns it.
➢ Symbols and Numbers are ignored.
Syntax: [Link]()
Ex:
>>> spam = 'Hello World!'
>>> spam = [Link]()
>>> spam
9
'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
numbers then returns “False”
Syntax: [Link]()
10
Ex:
a = "Hello World!"
b = "hello 123"
c = "MY NAME IS PETER"
print([Link]())
print([Link]())
print([Link]())
OUTPUT:
False
False
True
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.
➢ Digits and symbols return “True” but if the string contains only digits and
numbers then returns “False”.
Syntax: [Link]()
11
Ex:
a = "Hello World!"
b = "hello 123"
c = "my name is peter"
print([Link]())
print([Link]())
print([Link]())
OUTPUT:
False
False
True
Startswith()
The startswith() method returns True if the string starts with the specified value,
otherwise False.
13
Syntax: string_name.startswith(search_string, start, end)
Parameters:
search_string: [Link] 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 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([Link]('Python'))
Output:
True
Ex 2:
Output:
False
endswith()
14
➢ We may use them when we want only some particular substring of the
original string to be considered for searching.
Parameters :
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([Link]('fun'))
Output:
True
Ex:
message = 'Python is fun'
# check if the message starts with Python
print([Link]('Fun'))
Output:
False
15
The join() and split() String Methods
➢ 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:
[Link](iterable)
Example
x = ['cats', 'rats', 'bats']
xj = ', '.join(x)
print(xj)
Output:
cats, rats, bats
Example
print(x)
Output:
John#Peter#Vicky
16
Python 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:
Ex
txt = "apple#banana#cherry#orange"
# setting the maxsplit parameter to 1, will return a list with 2 elements!
x = [Link]("#", 1)
print(x)
Output:
['apple', 'banana#cherry#orange']
<class 'list'>
17
Justifying Text with rjust(), ljust(), and center()
Python 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
[Link](length, character)
character→ Optional. A character to fill the missing space (to the left of the
string).
Default is " " (space).
Ex.
txt = "banana"
x = [Link](20) # Return a 20 characters long, right justified version of the
word "banana":
Output:
txt = "banana"
x = [Link](20, “0”)
print(x)
Output:
OOOOOOOOOOOOOObanana
18
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
[Link](length, character)
length → Required. The length of the returned string
character → Optional. A character to fill the missing space (to the right of the
string).
Default is " " (space).
Ex:
txt = "banana"
x = [Link](20,'-')
print(x, "is my favorite fruit.")
Output:
Ex:
txt = "banana"
x = [Link](20, "O")
print(x)
Output:
19
bananaOOOOOOOOOOOOOO
Python String center() Method
The center() method will center align the string, using a specified character (space is
default) as the fill character.
Syntax
[Link](length, character)
Ex:
txt = "banana"
x = [Link](20, "O")
print(x)
Output:
OOOOOOObananaOOOOOOO
Syntax
[Link](characters)
Output:
of all fruits banana is my favorite
Ex:
txt = ",,,,,rrttgg.....banana....rrr"
x = [Link](",.grt")
print(x)
Output:
banana
The rstrip() method removes any trailing characters (characters at the end a string),
space is the default trailing character to remove.
Syntax:
[Link](characters)
characters → Optional. A set of characters to remove as trailing characters.
Ex:
txt = " banana "
x = [Link]() #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
21
Ex:
txt = "banana,,,,,ssqqqww....."
x = [Link](",.qsw") #Removes the trailing characters
print(x)
Output:
banana
Ex:
txt = " banana "
x = [Link]()
print("of all the fruits", x, "is my favorite")
Output:
of all fruits banana is my favorite
Ex
txt = ",,,,,ssaaww.....banana"
x = [Link](",.asw")
print(x)
Output:
banana
22
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 your 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:
23
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
24
* Lists of cultivars
26
Reading and Writing Files
Files and File Paths
➢ A file has two key properties: a filename (usually written as one word) and a
path.
➢ The path specifies the location of a file on the computer.
➢ For example, there is a file on my Windows 7 laptop with the filename
[Link] in the path C:\Users\asweigart\Documents.
➢ The part of the filename after the last period is called the file’s extension and
tells you a file’s type.
➢ [Link] is a Word document, and Users, asweigart, and Documents all
refer to folders (also called directories).
➢ Folders can contain files and other folders.
➢ For example, [Link] is in the Documents folder, which is inside the
asweigart folder, which is inside the Users folder.
Figure 8-1 shows this folder organization.
➢ The C:\ part of the path is the root folder, which contains all other folders.
➢ On Windows, the root folder is named C:\ and is also called the C: drive.
➢ On OS X and Linux, the root folder is /.
27
➢ 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.
➢ 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 [Link]() function.
➢ If you pass it the string values of individual file and folder names in your path,
[Link]() will return a string with a file path using the correct path
separators.
Enter the following into the interactive shell:
➢ The [Link]() function is helpful if you need to create strings for filenames.
➢ These strings will be passed to several of the file-related functions.
28
For example, the following example joins names from a list of filenames to the end of a
folder’s name:
Ex:
>>> myFiles = ['[Link]', '[Link]', '[Link]']
>>> for filename in myFiles:
print([Link]('C:\\Users\\asweigart', filename))
Output:
C:\Users\asweigart\[Link]
C:\Users\asweigart\[Link]
C:\Users\asweigart\[Link]
29
➢ When we change the current working directory to C:\Windows, [Link] is
interpreted as C:\Windows\[Link].
➢ Python will display an error if you try to change to a directory that does not
exist.
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.
30
Creating New Folders with [Link]()
A program can create new folders (directories) with the [Link]() 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, [Link]() will create any necessary intermediate folders in
order to ensure that the full path exists.
31
Figure 8-3 shows this hierarchy of folders.
32
Handling Absolute and Relative Paths
The [Link] module provides functions for returning the absolute path of a
relative path and for checking whether a given path is an absolute path.
Since C:\Python34 was the working directory when [Link]() was called, the
“single-dot” folder represents the absolute path 'C:\\Python34'.
Enter the following calls to [Link]() into the interactive shell:
33
Calling [Link](path) will return a string of everything that comes before the
last slash in the path argument.
Calling [Link](path) will return a string of everything that comes after the
last slash in the path argument.
The dir name and base name of a path are outlined in Figure 8-4.
34
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 [Link] module provides functions for finding the size of a file in bytes and the
files and folders inside a given folder.
Calling [Link](path) will return the size in bytes of the file in the path
argument.
Calling [Link](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 [Link].)
35
Checking Path Validity
Many Python functions will crash with an error if you supply them with a path that
does not exist.
The [Link] module provides functions to check whether a given path exists and
whether it is a file or folder.
Calling [Link](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 [Link](path) will return True if the path argument exists and is a file and
will return False otherwise.
Calling [Link](path) will return True if the path argument exists and is a folder
and will return False otherwise.
36
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.
➢ 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
37
There are three steps to reading or writing files in Python.
1. Call the open() function to return a File object.
2. Call the read() or write() method on the File object.
3. Close the file by calling the close() method on the File object
38
➢ Now, whenever you want to read from or write to the file, you can do so by
calling methods on the File object in helloFile.
Syntax:
[Link](size)
size Optional. The number of bytes to return. Default -1, which means the whole file.
Example:
f = open("[Link]", "r")
print([Link](33))
Output:
Hello! Welcome to [Link]
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:
[Link](size)
size Optional. The number of bytes from the line to return. Default -1, which
means the whole line
39
Example:
f = open("[Link]", "r")
[Link]
print([Link]())
Hello! Welcome to
[Link]
Output:
This file is for testing
Hello! Welcome to [Link]
purposes.
'Good Luck!'
f = open("[Link]", "r")
print([Link](5))
print([Link]())
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 hint 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:
[Link](hint)
hint Optional. If the number of bytes returned exceed the hint 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:
40
f = open("[Link]", "r")
print([Link]())
output:
['Hello! Welcome to [Link]\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("[Link]", "r")
print([Link](33))
Output:
['Hello! Welcome to [Link]\n', 'This file is for testing purposes.\n']
41
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
42
The write() method writes a specified text to the file.
Where the specified text will be inserted depends on the file mode and stream
position.
"a": The text will be inserted at the current file stream position, default at the end of
the file.
"w": The file will be emptied before the text will be inserted at the current file stream
position, default 0.
Syntax:
[Link](text)
Example:
f = open("[Link]", "a")
[Link]("See you soon!") [Link]
[Link]() Hello! Welcome to
[Link]
#open and read the file after the This file is for testing
appending: purposes.
f = open("[Link]", "r") 'Good Luck!'
print([Link]())
Output:
Hello! Welcome to [Link]
This file is for testing purposes.
Good Luck!See you soon!
43
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.
44
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 [Link]() 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.
45
➢ After running the previous code on Windows, you will see three new files in the
current working directory: [Link], [Link], and [Link].
➢ On OS X, only a single [Link] 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.
46
Saving Variables with the [Link]() Function
47
➢ Here, we import pprint to let us use [Link]().
➢ 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
[Link]() 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 [Link].
➢ The modules that an import statement imports are themselves just Python
scripts.
➢ When the string from [Link]() 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
48