COMP7004 - Systems Scripting
Lecture 17: More Python Strings and Copying to Clipboard
Dr. Vincent Emeakaroha
vincent.emeakaroha@mtu.ie
Semester 2, 2026
The startswith() and endswith()
String Methods
These methods are handy in situations where you want to
check if a string sentence starts with or ends with a particular
word.
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.
These methods can also be used to compare strings. In case
the string passed as argument to the method is equivalent to
the string object. They will return True.
See following example.
2
Example: startswith() and endswith()
3
The split() String Method
This method is very helpful in editing string sentences.
Since strings are immutable data types, the split()
method returns a list data type which is mutable and
could be edited.
The split() method is the opposite of a join() method
(shown later). It is called on a single string and returns
a list containing the string components.
By default, split() breaks down the strings using white
spaces. This behaviour can be changed or customised.
4
Example: split() String Method
You can pass a delimiter to the split() method to
specify a different string to split upon. See following
example.
5
split() String Method
A common usage of split() is to split multiline strings along
the newline characters.
6
The join() String Method
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 can be:
called on a string,
get passed a list of strings as arguments,
and returns a string.
The returned string is a concatenation of each string in the
passed-in argument list.
See following example:
7
Example: join() String Method
Notice that the string join() is called on, is inserted between each
string of the list argument.
Remember that join() is called on a string value and is passed a string
iterable data type (string, list, tuple, dictionary) and not the other way
round.
8
Justifying Text with rjust(), ljust()
Method
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 specifying the
length for the justified string. Note that the length of the string
called on is included in this value. See following examples.
9
Justifying Text with rjust(), ljust()
Method
An optional second argument to rjust() and ljust() will
specify a fill character other than a space character.
Example:
10
Justifying with center() Method
The center() string method works like ljust() and rjust() but
centres the text rather than justifying it to the left or right.
Example:
These methods are especially useful when you want to
print tabular data to have the correct spacing.
11
Sample Justifying Usage Scenario
Formatting and printing picnic items in tabular form.
12
Sample Usage Scenario: Output
13
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,
for example, to paste it on an email, word processor or some other
software.
This module is a third-party module and requires to be installed
before usage.
From OS X Terminal, issue command python3 -m pip3 install pyperclip
From Linux Terminal, issue sudo apt-get install python3-pip
You may need additional installations to get it working in Linux.
From Window command line interface, issue py m pip install pyperclip
14
Example: pyperclip Module
If something outside your program changes the
clipboard content, the paste() function will return it.
For example: (Just copied the above text to clipboard)
15
Pyperclip Usage Scenario: Password
locker
You probably have accounts on many different websites.
Its a bad habit to use the same password for each of them
because if any of those sites has a security breach, the
hacker will learn the password to all of your other
accounts.
It’s best to use password manager software on your
computer that uses one master password to unlock the
password manager. In this scenario, we want to create a
password manager (though not secure, does not have a
master password) that can help you manage password for
different websites.
16
Step 1: Program Design and Data
Structures
A user want to be able to run this program with a command
line argument that is the accounts name for instance, so
that the corresponding password will be copied to the
clipboard.
The user can then paste the content of the clipboard into
the password field to authenticate herself.
This way, the user can have long, complicated passwords
without having to memorise them.
Since we want to associate each accounts name with its
password, we can use dictionary to organise these data.
17
Password Locker: Data Structure
18
Step 2: Handle Command Line
Arguments
The command line arguments will be stored in the array
variable sys.argv.
The following describes the content of the sys.argv array:
First item is a string representing the program name (pw.py)
Second item is the first command line argument the name of
the particular account in our use case.
And so on.
Since the command line argument is mandatory, you
should display a usage message to the user if they forgot to
provide it.
19
Password Locker: Command Line Argument
20
Step 3: Copy the Right Password
Now that the account name is stored as a string in a variable
account, you need to see whether it exists in the password
dictionary as a key.
If so, you want to copy the keys value to the clipboard using
pyperclip.copy() function. (We need to import the pyperclip
module first)
Note that you do not actually need the account variable, you
could just use sys.argv[1] everywhere account is used in the
program.
But the account variable name is much more readable than
something cryptic like sys.argv[1].
21
Password Locker: Full Solution
22
Password Locker: Invocation
The program is designed to be called from the
command line.
Example invocation:
python3 pw.py email
In the above invocationemail represents the
account name. With this invocation, the password
associated with the specified email account is copied
to the clipboard and you can simply paste in the
password field.
23
Thank You!
24