Python Module 3
Python Module 3
Manipulating Strings
working with Strings:
String Literals:
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.
>>> spam = "That is Alice's cat."
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.
>>> spam = 'Say hi to Bob\'s mother.'
>>> print(‘Hello there!\nHow are you?\nI\'m doing fine.’)
Hello there!
How are you?
I'm doing fine.
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.
>>> print(r'That is Carol\'s cat.')
That is Carol\'s cat.
Multiline Strings with triple Quotes: While you can use the \n escape character to put a newline into a string, it is
often easier to use multiline strings. A multiline string in Python begins and ends with either three single quotes or
three double quotes. Any quotes, tabs, or newlines in between the “triple quotes” are considered part of the string.
Python’s indentation rules for blocks do not apply to lines inside a multiline string.
print('''Dear Alice,
Eve's cat has been arrested for catnapping, cat burglary, and extortion.
Sincerely,
Bob''')
Dear Alice,
Eve's cat has been arrested for catnapping, cat burglary, and extortion.
Sincerely,
Bob
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.
"""This is a test Python program.
Written by Al Sweigart [email protected]
This program was designed for Python 3, not Python 2.
"""
def spam():
"""This is a multiline comment to help
explain what the spam() function does."""
print('Hello!')
Indexing and Slicing Strings: Strings use indexes and slices the same way lists do. You can think of 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, so 'Hello world!' is 12 characters long, from H at index 0 to ! at
index 11.
>>> spam = 'Hello world!'
>>> spam[0]
'H'
>>> spam[4]
'o'
>>> spam[-1]
'!'
>>> spam[0:5]
'Hello'
>>> spam[:5]
'Hello'
>>> spam[6:]
'world!'
The in and not in Operators with Strings: The in and not in operators can be used with strings just like with list
values. An expression with two strings joined using in or not in will evaluate to a Boolean True or False.
>>> 'Hello' in 'Hello World'
True
>>> 'Hello' in 'Hello'
True
>>> 'HELLO' in 'Hello World'
False
>>> 'cats' not in 'cats and dogs'
False
useful String methods
The upper(), lower(), isupper(), and islower() String Methods: The upper() and lower() string methods return a
new string where all the letters in the original string have been converted to uppercase or lowercase, respectively.
Nonletter characters in the string remain unchanged.
>>> spam = 'Hello world!'
>>> spam = spam.upper()
>>> spam
'HELLO WORLD!'
>>> spam = spam.lower()
>>> spam
'hello world!'
The isupper() and islower() methods will return a Boolean True value if the string has at least one letter and all the
letters are uppercase or lowercase, respectively. Otherwise, the method returns False.
>>> spam = 'Hello world!'
>>> spam.islower()
False
>>> spam.isupper()
False
>>> 'HELLO'.isupper()
True
>>> 'abc12345'.islower()
True
>>> '12345'.islower()
False
>>> '12345'.isupper()
False
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 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 new
lines 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.
>>> 'hello'.isalpha()
True
>>> 'hello123'.isalpha()
False
>>> 'hello123'.isalnum()
True
>>> 'hello'.isalnum()
True
>>> '123'.isdecimal()
True
>>> ' '.isspace()
True
>>> 'This Is Title Case'.istitle()
True
>>> 'This Is Title Case 123'.istitle()
True
>>> 'This Is not Title Case'.istitle()
False
>>> 'This Is NOT Title Case Either'.istitle()
False
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.')
The startswith() and endswith() String Methods: The startswith() and endswith() methods return True if the string
value they are called on begins or ends (respectively) with the string passed to the method; otherwise, they return
False.
>>> 'Hello world!'.startswith('Hello')
True
>>> 'Hello world!'.endswith('world!')
True
>>> 'abc123'.startswith('abcdef')
False
>>> 'abc123'.endswith('12')
False
>>> 'Hello world!'.startswith('Hello world!')
True
>>> 'Hello world!'.endswith('Hello world!')
True
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 is called on a string, gets passed a list of strings, and
returns a string. The returned string is the concatenation of each string in the passed-in list.
>>> ', '.join(['cats', 'rats', 'bats'])
'cats, rats, bats'
>>> ' '.join(['My', 'name', 'is', 'Simon'])
'My name is Simon'
>>> 'ABC'.join(['My', 'name', 'is', 'Simon'])
'MyABCnameABCisABCSimon'
The split() method does the opposite: It’s called on a string value and returns a list of strings.
>>> 'My name is Simon'.split()
['My', 'name', 'is', 'Simon']
>>> 'MyABCnameABCisABCSimon'.split('ABC')
['My', 'name', 'is', 'Simon']
>>> 'My name is Simon'.split('m')
['My na', 'e is Si', 'on']
Justifying Text with rjust(), ljust(), and center():The rjust() and ljust() string methods return a padded version of
the string they are called on, with spaces inserted to justify the text. The first argument to both methods is an integer
length for the justified string.
>>> 'Hello'.rjust(10)
' Hello'
>>> 'Hello'.rjust(20)
' Hello'
>>> 'Hello World'.rjust(20)
' Hello World'
>>> 'Hello'.ljust(10)
'Hello '
>>> 'Hello'.rjust(20, '*')
'***************Hello'
>>> 'Hello'.ljust(20, '-')
'Hello---------------'
>>> 'Hello'.center(20)
' Hello '
>>> 'Hello'.center(20, '=')
'=======Hello========'
Removing Whitespace with strip(), rstrip(), and lstrip(): Sometimes you may want to strip off whitespace
characters (space, tab, and newline) from the left side, right side, or both sides of a string. The strip() string method
will return a new string without any whitespace characters at the beginning or end. The lstrip() and rstrip() methods
will remove whitespace characters from the left and right ends, respectively.
>>> spam = ' Hello World '
>>> spam.strip()
'Hello World'
>>> spam.lstrip()
'Hello World '
>>> spam.rstrip()
' Hello World'
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.
>>> import pyperclip
>>> pyperclip.copy('Hello world!')
>>> pyperclip.paste()
'Hello world!'
Project: Password locker
To use password manager software on your computer that uses one master password to unlock the password
manager. Then you can copy any account password to the clipboard and paste it into the website’s Password field.
The Current Working Directory:Every program that runs on your computer has a current working
directory, or cwd. Any filenames or paths that do not begin with the root folder are assumed to be under
the current working directory. You can get the current working directory as a string value with the
os.getcwd() function and change it with os.chdir().
>>> import os
>>> os.getcwd()
'C:\\Python34'
>>> os.chdir('C:\\Windows\\System32')
>>> os.getcwd()
'C:\\Windows\\System32'
Absolute vs. Relative Paths: 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 the program’s current working
directory
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.”
Creating New Folders with os.makedirs():
>>> import os
>>> os.makedirs('C:\\pythonlanguage\\program\\details')
This will create not just the C:\pythonlangauge folder but also a program folder inside C:\pythonlanguage
and a details folder inside C:\pythonlanguage\program. That is, os.makedirs() will create any necessary
intermediate folders in order to ensure that the full path exists.
The os.path module:The os.path module contains many helpful functions related to filenames and file
paths. For instance, you’ve already used os.path.join() to 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.
Handling Absolute and Relative Paths:The os.path module provides functions for returning the
absolute path of a relative path and for checking whether a given path is an absolute path.
• Calling os.path.abspath(path) will return a string of the absolute path of the argument. This is an
easy way to convert a relative path into an absolute one.
• Calling os.path.isabs(path) will return True if the argument is an absolute path and False if it is a
relative path.
• 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.
>>> os.path.abspath('.')
'C:\\Python34'
>>> os.path.abspath('.\\Scripts')
'C:\\Python34\\Scripts'
>>> os.path.isabs('.')
False
>>> os.path.isabs(os.path.abspath('.'))
True
>>> os.path.relpath('C:\\Windows', 'C:\\')
'Windows'
>>> os.path.relpath('C:\\Windows', 'C:\\spam\\eggs')
'..\\..\\Windows'
>>> os.getcwd()
'C:\\Python34'
Calling os.path.dirname(path) will return a string of everything that comes before the last slash in the
path argument. Calling os.path.basename(path) will return a string of everything that comes after the last
slash in the path argument.
>>> path = 'C:\\Windows\\System32\\calc.exe'
>>> os.path.basename(path)
'calc.exe'
>>> os.path.dirname(path)
'C:\\Windows\\System32'
If you need a path’s dir name and base name together, you can just call os.path.split() to get a tuple value
with these two strings, like so:
>>> calcFilePath = 'C:\\Windows\\System32\\calc.exe'
>>> os.path.split(calcFilePath)
('C:\\Windows\\System32', 'calc.exe')
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.
• 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.)
>>> os.path.getsize('C:\\Windows\\System32\\calc.exe')
776192
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.
• 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.
>>> os.path.exists('C:\\Windows')
True
>>> os.path.exists('C:\\some_made_up_folder')
False
>>> os.path.isdir('C:\\Windows\\System32')
True
>>> os.path.isfile('C:\\Windows\\System32')
False
>>> os.path.isdir('C:\\Windows\\System32\\calc.exe')
False
>>> os.path.isfile('C:\\Windows\\System32\\calc.exe')
True
The file reading/writing Process: 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. Your 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.
Reading the Contents of Files:Now that you have a File object, you can start reading from it. If you
want to read the entire contents of a file as a string value, use the File object’s read() method. Let’s
continue with the hello.txt File object you stored in helloFile .
>>> helloContent = helloFile.read()
>>> helloContent
'Hello world!'
Writing to Files
Python allows you to write content to a file in a way similar to how the print() function “writes” strings
to the screen. You can’t write to a file you’ve opened in read mode, though. Instead, you need to open it in
“write plaintext” mode or “append plaintext” mode, or write mode and append mode for short. 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. Append
mode, on the other hand, will append text to the end of the existing file. You can think of this as
appending to a list in a variable, rather than overwriting the variable altogether. Pass 'a' as the second
argument to open() to open the file in append mode.
If the filename passed to open() does not exist, both write and append mode will create a new, blank file.
After reading or writing a file, call the close() method before opening the file again.
>>>helloFile = open('hello.txt', 'w')
>>> helloFile.write('Hello world!\n')
13
>>> helloFile.close()
>>> helloFile = open('hello.txt', 'a')
>>> helloFile.write('Have a good day.')
16
>>> helloFile.close()
>>> helloFile = open('hello.txt')
>>> content = helloFile.read()
>>> helloFile.close()
>>> print(content)
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.
>>> import shelve
>>> shelfFile = shelve.open('mydata')
>>> cats = ['Zophie', 'Pooja', 'Simon']
>>> shelfFile['cats'] = cats
>>> shelfFile.close()
Saving Variables with the pprint.pformat() function: pprint.pformat() function will return this same
text as a string instead of printing it. Not only is this string formatted to be easy to read, but it is also
syntactically correct Python code. Say you have a dictionary stored in a variable and you want to save this
variable and its contents for future use. Using pprint.pformat() will give you a string that you can write
to .py file. This file will be your very own module that you can import whenever you want to use the
variable stored in it.
>>> import pprint
>>> cats = [{'name': 'Zophie', 'desc': 'chubby'}, {'name': 'Pooja', 'desc': 'fluffy'}]
>>> pprint.pformat(cats)
"[{'desc': 'chubby', 'name': 'Zophie'}, {'desc': 'fluffy', 'name': 'Pooja'}]"
>>> fileObj = open('myCats.py', 'w')
>>> fileObj.write('cats = ' + pprint.pformat(cats) + '\n')
83
>>> fileObj.close()