Python Module 3
Python Module 3
MANIPULATING STRINGS
1
Working with Strings
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.
String Literals
● Can we use a quote inside a string?
'That is Alice's cat.'
● Double quotes
"That is Alice's cat."
● Escape character
'Say hi to Bob\'s mother.'
5
print( ) using escape characters
& raw strings
print("Hello there!\nHow are you?\nI\'m doing fine.")
A raw string completely ignores all escape characters and prints any backslash
that appears in the string.
Python’s indentation rules for blocks do not apply to lines inside a multiline string.
9
# Multiline
string using
single quote
# Multiline
string using
double quote
Without using multiline string
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.
12
Indexing and Slicing Strings
'Hello, world!'
13
Slicing string
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.
15
Putting Strings Inside Other Strings
16
Built-in methods for string
manipulation
Useful String Methods
20
Nonletter characters in the string remain changed or unchanged
Asw: Unchanged
❑String 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() and
lower() on the string and then assign the new string to the variable
where the original was stored.
spam='Hello world!'
spam.upper()
spam
Output:
Hello world!
Guess the output:
for i in range(2,2):
spam='Hello world!'
print(i)
spam.upper()
spam
Output:
It will not print anything
Output:
Hello world!
When do we use upper ()and lower()
spam='Hello world!'
methods?
spam=spam.upper()
These methods are helpfull when you do
Spam
a case-insensitive comparison.
Output:
HELLO WORLD!
isX() Methods
●
isalpha() Returns True if the string consists only of letters and isn’t 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
24
isX( ) examples
25
' '.isalpha()
'123'.isalnum()
False
True
'helloHello'.isalpha()
'abcAbc'.isalnum()
True
True
'hello123'.isalnum()
'This is String Methods'.istitle()
True
False
'01234'.isdecimal()
'This Is String Methods'.istitle()
True
True
'01234c'.isdecimal()
'This Is String METHODS'.istitle()
False
False
'hello123!'.isalnum()
False
‘ ’isspace()
True
‘\n’.isspace()
True
‘ My name ’.isspace()
False
‘ \n ’.isspace()
True
‘ \n hi ’.isspace()
False
The isX methods are helpful when you need to validate user
input.
Example:
The following program repeatedly asks user for their age and
a password until they provide valid input.
In the first while loop, we ask the user
for their age and store their
input in age. If age is a valid (decimal)
value, we break out of this first while
loop and move on to the second, which
asks for a password. Otherwise, we
inform the user that they need to enter a
number and again ask them to enter
their age.
In the second while loop, we ask for a
password, store the user’s input in
password, and break out of the loop if
the input was alphanumeric.
If it wasn’t, we’re not satisfied so we tell
the user the password needs to be
alphanumeric and again ask them to
enter a password.
Calling isdecimal() and isalnum()
on variables, we’re able to test
whether the values stored in those
variables are decimal or not,
alphanumeric or not. Here, these
tests help us reject the input forty
two and accept 42, andreject
secr3t! and accept secr3t.
startswith() and endswith()
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.
31
These methods are useful alternatives to the ‘==‘ operator.
33
split ( ) method
It does opposite to join()
34
split ( ) using a delimiter
A common use of
split() is to split a
multiline string along
the newline
characters.
35
'My name is simon'.split('m')
spam='''Dear Alice
Output:
How are you doing?
['My na', 'e is si', 'on‘]
Please take care of your health'''
spam.split('\n')
spam='''Dear Alice
How are you doing?
Please take care of your health''' Output:
spam.split()
['Dear Alice', 'How are you doing?', 'Please
take care of your health']
Output:
['Dear', 'Alice', 'How', 'are', 'you',
'doing?', 'Please', 'take', 'care',
'of', 'your', 'health']
Splitting Strings with the partition() Method
If the separator string you pass to partition() occurs multiple times in the string that partition()
calls on, the method splits the string only on the first occurrence:
37
Using partition( ) for multiple assignment
38
>>>fruit,sep,vegetable='Apple Carrot'.partition(‘ ')
>>>vegetable
‘Carrot’
>>>fruit
‘Apple’
Justifying Text
rjust()
ljust()
center()
40
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.
The ord() function can be used to get the code point of a one-character string, and the
chr() function to get the one-character string of an integer code point
46
Ordering or mathematical operation
on characters
47
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.
Output:
The prime numbers are: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61,
67, 71, 73, 79, 83, 89, 97]
Project
Reading and Writing Files
File Handling in Python
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 projects.docx 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.
❑project.docx is a Word document, and Users,
asweigart, and Documents all refer to folders/
directories
❑Folders can contain files and other folders.
For example, project.docx is in the Documents
folder, which is inside the asweigart folder, which is
inside the Users folder.
❑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 /.
Backslash on Windows and Forward Slash on OS X and 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 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.
54
❑ os.path.join('usr', 'bin', 'spam') returned 'usr\\bin\\
spam‘ in windows.
❑ If I had called this function on 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.
56
File handling commands in UNIX
●
pwd : present working directory
●
ls : list files
●
Home Directory (/)
●
cd : change directory
●
cd / : change to root directory
●
mkdir : make directory / create directory
●
Copy and move commands: cp and mv
57
pathlib module in python
● A built-in module called pathlib is available in python 3.4
onwards for handling files.
58
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().
●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.”
61
(dot) and (dot dot) operators
. (this directory)
Demo in Terminal .. (parent directory)
62
Creating New Folders with os.makedirs()
68
dirname and basename
os.path.split()
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:
os.path.sep()
Finding File Sizes and Folder Contents
75
The File Reading/Writing Process
❑ The functions covered in the next few sections will apply to plaintext files.
❑ 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.
❑ 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
Three steps to reading or writing files in Python
79
Binary Files
● Opening a calc.exe in windows using notepad
80
Opening Files with the open() Function
To open a file with the open() function, you pass it a string path indicating
the file you want to open; it can be either an absolute or relative path. The
open() function returns a File object.
open('/Users/asweigart/hello.txt', 'r')
Reading the Contents of Files
If you want to read the entire contents of a file
as a string value, use the File object’s read()
method.
use the readlines() method to get a list of string
values from the file, one string for each line of
text.
To read the content of the file line by line there are 3
commands:
Writing to Files
Before After
5. Develop a program to print 10 most frequently appearing words in a text file. [Hint:
Use dictionary with distinct words and their frequency of occurrences. Sort the
dictionary in the reverse order of frequency and display dictionary slice of first 10
items]
OUTPUT