Python Basics 5#
Description: This notebook focuses on strings, preparing learners to use:
Escape characters
String methods
The print()
function#
Often when working with strings, we use the print()
function. A deeper understanding of print()
will help us work with strings more flexibly.
Escape characters#
Python strings can use single or double quotes. If the string contains a single quote character, it may be beneficial to use double quotes. Try printing out the string in the next code cell:
# Print out a string using single or double quotes
string = 'Hello World: Here's a string.'
print(string)
Cell In[1], line 2
string = 'Hello World: Here's a string.'
^
SyntaxError: invalid syntax
An easy solution would be to use double quotes, such as:
string = “Hello World: Here’s a string.”
The use of double quotes keeps Python from ending the string prematurely. But what if your string contains both single and double quotes? Escape characters help us insert certain characters into a string. An escape character begins with a \
. For example, we could insert a single quote into a string surrounded by single quotes by using an escape character.
# Print out a single quote in a Python string
string = 'There\'s an escape character in this string.'
print(string)
The backslash character \
in front of the single quote tells Python not to end the string prematurely. Of course, this opens a new question: How do we create a string with a backslash? The answer is another escape character using two backslashes.
# Print a backslash using an escape character
string = 'Adding a backslash \\ requires an escape character.'
print(string)
Another option is to use a raw string, which ignores any escape characters. A raw string simply starts with an r
similar to an f
string.
string = r'No escape characters \ here'
print(string)
Escape characters also do more than just allow us to add quotes and backslashes. They are also responsible for string formatting for aspects such as tabs and new lines.
Code |
Result |
---|---|
|
‘ |
|
\ |
|
tab |
|
new line |
# Print out a string with two lines
# Print out a string with a tab
Try it! < / >
Can you print a string with a new line? How about a tab?
The newline escape character \n
can affect readability for many lines. Consider this string containing four lines of a Shakespeare sonnet.
string = 'Shall I compare thee to a summer’s day?\nThou art more lovely and more temperate:\nRough winds do shake the darling buds of May,\nAnd summer’s lease hath all too short a date;\n'
print(string)
A more readable option is to create a string with a triple quote (single or double). This string type can also automatically interpret new lines and tabs.
# Print out Shakespeare's Sonnet 18
string = """Shall I compare thee to a summer’s day?
Thou art more lovely and more temperate:
Rough winds do shake the darling buds of May,
And summer’s lease hath all too short a date;
Sometime too hot the eye of heaven shines,
And often is his gold complexion dimm'd;
And every fair from fair sometime declines,
By chance or nature’s changing course untrimm'd;
But thy eternal summer shall not fade,
Nor lose possession of that fair thou ow’st;
Nor shall death brag thou wander’st in his shade,
When in eternal lines to time thou grow’st:
So long as men can breathe or eyes can see,
So long lives this, and this gives life to thee."""
print(string)
Formatted strings (f-strings)#
An f-string can help us concatenate a variable inside of string. Consider this example where a print function must concatenate three strings:
# Greeting a user with a concatenated string
username = input('Hi. What is your name? ')
print('Hello ' + username + '!')
We used the +
operator twice to concatenate username
between the strings 'Hello '
and '!'
. A simpler method would be to use an f-string. Similar to the way a raw string begins with an r r'string'
, the formatted string begins with an f f'string'
. The variable to be concatenated is then included in curly brackets {}
.
# Print the username inside a formatted string
print(f'Hello {username}!')
Using print()
with a sep
or end
argument#
The print()
function can accept additional arguments such as sep
or end
. These can help format a string appropriately for output. By default, the print function will print many objects separated by a comma.
# Print multiple objects with a single print() statement
string1 = 'Hello'
string2 = 'World'
string3 = '!'
print(string1, string2, string3)
Notice that the print()
function defaults to include a single space separator between the objects it prints. We can change this default separator by using the sep
parameter.
# Use a plus as a separator
print(string1, string2, string3, sep='+')
We can even remove the separator by specifying an empty string.
# Specify an empty string for no separation
print(string1, string2, string3, sep='')
The print print()
function also concatenates a new line by default. The is specified in the default argument end='\n'
.
# Two strings printed on separate lines
print('Hello')
print('World')
Try it! < / >
Keeping both print()
functions above, can you print the outputs on the same line?
String slices and methods#
String slices#
The characters of a string can also be indexed and sliced like the items of a list.
# Using a string index
string = 'Python Basics'
string[0]
# Slicing a string
string = 'Python Basics'
string[0:6]
We can use flow control on a string the same way we would with a list.
# Use a for loop on the string
# To print each character except any letter 'o'
string = 'Hello World'
Try it! < / >
Can you use a for
loop to print each character of the string Hello World
without printing the letter ‘o’?
String methods#
There are a variety of methods for manipulating strings.
Method |
Purpose |
Form |
---|---|---|
.lower() |
change the string to lowercase |
string.lower() |
.upper() |
change the string to uppercase |
string.upper() |
.join() |
joins together a list of strings |
‘ ‘.join(string_list) |
.split() |
splits strings apart |
string.split() |
.replace() |
replaces characters in a string |
string.replace(oldvalue, newvalue) |
.rjust(), .ljust(), .center() |
pad out a string |
string.rjust(5) |
.rstrip(), .lstrip(), .strip() |
strip out whitespace |
string.rstrip() |
All of the characters in a string can be lowercased with .lower()
or uppercased with .upper()
.
# Lowercase a string
string = 'Hello World'
string.lower()
These methods do not change the original string, but they return a string that can be saved to a new variable.
# The original string is unchanged
print(string)
# The returned string can be assigned to a new variable
new_string = string.upper()
print(new_string)
A string can be split on any character, or set of characters, passed into .split()
. By default, strings are split on any whitespace including spaces, new lines, and tabs.
# Splitting a string on white space
string = 'This string will be split on whitespace.'
string.split()
# Splitting a phone string based on the '-' character
phone_string = '313-555-3434'
phone_string.split('-')
Similarly, lists of strings can be joined together by passing them into .join()
. A joining string must be specified before the .join()
, even if it is the empty string ''
.
# List of strings joined together
name_list = ['Sam', 'Delilah', 'Jordan']
', '.join(name_list)
The .strip()
method will strip leading and trailing whitespace (including spaces, tabs, and new lines) from a string. Remember, these changes will not affect the original string, but they can be assigned to a new variable.
# Stripping leading and trailing whitespaces from a string
string = ' Python Basics '
string.strip()
It is also possible to only strip whitespace from the right or left of a string.
# Stripping leading whitespace from the leftside of a string
string = ' Python Basics '
string.lstrip()
Characters in a string can be replaced with other characters using the .replace()
method.
# Replacing characters in a string with .replace()
string = 'Hello world'
string.replace('l', 'x')
# Removing characters from a string
# using .replace with an empty string
string = 'Hello! World!'
string.replace('!', '')
Finally, strings can be justified (or padded out) with characters leading, trailing, or both. By default, strings are justified with spaces but other characters can be specified by passing a second argument.
# Left justifying a string
string1 = 'Hello'
string2 = 'world!'
print(string1.ljust(10) + string2)
# Left justifying a string with pluses
string1 = 'Hello'
string2 = 'world!'
print(string1.ljust(10, '+') + string2)
# Right justifying a string
string1 = 'Hello'
string2 = 'world!'
print(string1 + string2.rjust(10))
# Center a string
string = 'Hello world!'
print('|' + string.center(20) + '|')
# Center a string
string = 'Hello world!'
print('|' + string.center(20, '+') + '|')
# Printing a dictionary of contacts in neat columns
contacts ={
'Amanda Bennett': 'Engineer, electrical',
'Bryan Miller': 'Radiation protection practitioner',
'Christopher Garrison': 'Planning and development surveyor',
'Debra Allen': 'Intelligence analyst'}
print('Name', 'Occupation')
for name, occupation in contacts.items():
print(name, occupation)
Try it! < / >
Can you clean up the printing of the contacts dictionary?
Checking string contents#
There are a variety of ways to to verify the contents of a string. These return a Boolean True
or False
and are useful for flow control. For example, we can check if a particular set of characters is inside of a string with the in
and not in
operators. The result is a Boolean True or False.
# Check whether a set of characters can be found in a string
string = 'Python Basics'
'Basics' in string
The following string methods also return Boolean True
or False
values.
Method |
Purpose |
Form |
---|---|---|
.startswith(), .endswith() |
returns |
string.startswith(‘abc’) |
.isupper(), .islower() |
returns |
string.isupper() |
.isalpha() |
returns |
string.isalpha() |
.isalnum() |
returns |
string.alnum() |
.isdigit() |
returns |
string.isdigit() |
# Checking if a string starts
# with a particular set of characters
string = 'Python Basics'
string.startswith('Python')
# Checking if a string is lowercased
string = 'python basics'
string.islower()
# Checking if a string is alphabet characters
string = 'PythonBasics'
string.isalpha()
# Checking if a string only
# alphabetic characters and numbers
string = 'PythonBasics5'
string.isalnum()
# Checking if a string is only numbers
string = '50'
string.isdigit()
The .isdigit()
method checks each character to verify it is a digit between 0-9. It will return false
if there is a negative (-) or decimal point (.) character.
Coding Challenge! < / >
Use flow control on the staff list below to print the name of every person with the first name ‘Patricia’ or the the last name ‘Mitchell’
# A list of staff members
staff = ['Tara Richards',
'Tammy French',
'Justin Douglas',
'Lauren Marquez',
'Aaron Wilson',
'Dennis Howell',
'Brandon Reed',
'Kelly Baker',
'Justin Howard',
'Sarah Myers',
'Vanessa Burgess',
'Timothy Davidson',
'Jessica Lee',
'Christopher Miller',
'Lisa Grant',
'Ryan Chan',
'Gary Carson',
'Anthony Mitchell',
'Jacob Turner',
'Jennifer Bonilla',
'Rachel Gonzalez',
'Patricia Clark',
'Richard Pearson',
'Glenn Allen',
'Jacqueline Gallagher',
'Carlos Mcdowell',
'Jeffrey Harris',
'Danielle Mitchell',
'Sarah Craig',
'Vernon Vasquez',
'Anthony Burton',
'Erica Bryant',
'Patricia Walker',
'Karen Brown',
'Terri Walker',
'Michelle Knight',
'Kathleen Douglas',
'Debbie Estrada',
'Jennifer Brewer',
'Taylor Rodriguez',
'Lisa Turner',
'Julie Hudson',
'Christina Cox',
'Nancy Patrick',
'Patricia Mosley',
'Nicholas Gordon',
'Wanda Vasquez',
'Jason Lopez',
'Anna Mitchell',
'Tyler Perez']
Coding Challenge! < / >
Print all the first names in the staff list. The .split()
method would be useful.
# Print all the first names in the staff list
Coding Challenge! < / >
Use flow control on the staff list to add all of the staff list to the colleagues list below. You will need to split the first and last names apart using the .split()
method. You will also need to create a dictionary for each entry in the colleagues list and append the dictionary to colleagues list. Print out the first names of all colleagues.
colleagues = [
{'first_name': 'Ada', 'last_name': 'Lovelace'},
{'first_name': 'Charles', 'last_name': 'Babbage'}
]
# Print the first name of the first colleague
print(colleagues[0]['first_name'])
Attribution
Created by Nathan Kelber and Ted Lawless for JSTOR Labs under Creative Commons CC BY License
Coding Solutions#
Here are a few solutions for exercises in this lesson.
# Using a for loop on a string
string = 'Hello World'
for character in string:
if character != 'o':
print(character, end='')
# Printing a dictionary of contacts in neat columns
contacts ={
'Amanda Bennett': 'Engineer, electrical',
'Bryan Miller': 'Radiation protection practitioner',
'Christopher Garrison': 'Planning and development surveyor',
'Debra Allen': 'Intelligence analyst'}
print('Name'.ljust(22), 'Occupation')
print('|'.center(44, '-'))
for name, occupation in contacts.items():
print(name.ljust(20), '|', occupation)
# Print all staff names with the first name Patricia or last name Mitchell
for name in staff:
if name.startswith('Patricia') or name.endswith('Mitchell'):
print(name)
# Print all the first names in the staff list
for name in staff:
print(name.split()[0])
# Add the first and last names of the staff list to the colleagues list
colleagues = [
{'first_name': 'Ada', 'last_name': 'Lovelace'},
{'first_name': 'Charles', 'last_name': 'Babbage'}
]
# Add staff names to colleagues list
for name in staff:
colleague_dict = {}
first_name = name.split()[0]
last_name = name.split()[1]
colleague_dict['first_name'] = first_name
colleague_dict['last_name'] = last_name
colleagues.append(colleague_dict)
for entry in colleagues:
print(entry['first_name'])
# Verify all the colleagues were added
#from pprint import pprint
#pprint(colleagues)