Week 4 and 5 Sequences Strings Lists and Files
Week 4 and 5 Sequences Strings Lists and Files
Week 5
8-1
Topics
• Basic String Operations
• String Slicing
• Testing, Searching, and Manipulating Strings
8-2
Basic String Operations
• Many types of programs perform operations on strings
• In Python, many tools for examining and manipulating
strings
– Strings are sequences, so many of the tools that work
with sequences work with strings
8-3
Python Data Typs
8-4
String Concatenation
• Concatenation: appending one string to the end of
another string
– Use the + operator to produce a string that is a
combination of its operands
– The augmented assignment operator += can also be
used to concatenate strings
The operand on the left side of the += operator must be
an existing variable; otherwise, an exception is raised
8-5
Strings Are Immutable (1 of 2)
• Strings are immutable
– Once they are created, they cannot be changed
Concatenation doesn’t actually change the existing
string, but rather creates a new string and assigns the
new string to the previously used variable
– Cannot use an expression of the form
– string[index] = new_character
Statement of this type will raise an exception
8-6
Strings Are Immutable (2 of 2)
8-7
String Slicing
• Slice: span of items taken from a sequence, known as
substring
– Slicing format: string[start : end]
Expression will return a string containing a copy of the
characters from start up to, but not including, end
If start not specified, 0 is used for start index
If end not specified, len(string) is used for end index
– Slicing expressions can include a step value and
negative indexes relative to end of string
8-8
Testing, Searching, and Manipulating
Strings
• You can use the in operator to determine whether
one string is contained in another string
– General format: string1 in string2
string1 and string2 can be string literals or
variables referencing strings
• Similarly you can use the not in operator to
determine whether one string is not contained in
another string
8-9
String Method
• Python string methods is a collection of in-built Python
functions that operate on lists.
• Note: Every string method in Python does not change
the original string instead returns a new string with the
changed attributes.
8 - 10
Example – String Method
• The below Python functions are used to change the case of
the strings. Let’s look at some Python string methods with
examples:
• lower(): Converts all uppercase characters in a string into
lowercase
• upper(): Converts all lowercase characters in a string into
uppercase
• title(): Convert string to title case
• swapcase(): Swap the cases of all characters in a string
• capitalize(): Convert the first character of a string to
8 - 11
uppercase
String Methods (1 of 7)
• Strings in Python have many types of methods,
divided into different types of operations
– General format:
mystring.method(arguments)
• Some methods test a string for specific characteristics
– Generally Boolean methods, that return True if a
condition exists, and False otherwise
8 - 12
String Methods (3 of 7)
• Some methods return a copy of the string, to which
modifications have been made
– Simulate strings as mutable objects
• String comparisons are case-sensitive
– Uppercase characters are distinguished from
lowercase characters
– lower and upper methods can be used for making
case-insensitive string comparisons
8 - 13
String Methods (2 of 7)
Table 5-1 Some string testing methods
Method Description
isalnum() Returns true if the string contains only alphabetic letters or digits and is at least one
character in length. Returns false otherwise.
isalpha() Returns true if the string contains only alphabetic letters and is at least one
character in length. Returns false otherwise.
isdigit() Returns true if the string contains only numeric digits and is at least one character in
length. Returns false otherwise.
islower() Returns true if all of the alphabetic letters in the string are lowercase, and the string
contains at least one alphabetic letter. Returns false otherwise.
isspace() Returns true if the string contains only whitespace characters and is at least one
character in length. Returns false otherwise. (Whitespace characters are spaces,
newlines (\n), and tabs (\t).
isupper() Returns true if all of the alphabetic letters in the string are uppercase, and the string
contains at least one alphabetic letter. Returns false otherwise.
8 - 14
String Methods (4 of 7)
Table 8-2 String Modification Methods
Method Description
lower() Returns a copy of the string with all alphabetic letters converted to lowercase. Any
character that is already lowercase, or is not an alphabetic letter, is unchanged.
lstrip() Returns a copy of the string with all leading whitespace characters removed. Leading
whitespace characters are spaces, newlines (\n), and tabs (\t) that appear at the
beginning of the string.
lstrip(char) The char argument is a string containing a character. Returns a copy of the string with all
instances of char that appear at the beginning of the string removed.
rstrip() Returns a copy of the string with all trailing whitespace characters removed. Trailing
whitespace characters are spaces, newlines (\n), and tabs (\t) that appear at the end of
the string.
rstrip(char) The char argument is a string containing a character. The method returns a copy of the
string with all instances of char that appear at the end of the string removed.
strip() Returns a copy of the string with all leading and trailing whitespace characters removed.
strip(char) Returns a copy of the string with all instances of char that appear at the beginning and
the end of the string removed.
upper() Returns a copy of the string with all alphabetic letters converted to uppercase. Any
character that is already uppercase, or is not an alphabetic letter, is unchanged.
8 - 15
String Methods (5 of 7)
• Programs commonly need to search for substrings
• Several methods to accomplish this:
– endswith(substring): checks if the string ends
with substring
Returns True or False
– startswith(substring): checks if the string starts
with substring
Returns True or False
8 - 16
String Methods (6 of 7)
• Several methods to accomplish this (cont’d):
– find(substring): searches for substring within
the string
Returns lowest index of the substring, or if the substring
is not contained in the string, returns -1
– replace(substring, new_string):
Returns a copy of the string where every occurrence of
substring is replaced with new_string
8 - 17
The Repetition Operator
• Repetition operator: makes multiple copies of a string
and joins them together
– The * symbol is a repetition operator when applied to a
string and an integer
String is left operand; number is right
– General format: string_to_copy * n
– Variable references a new string which contains
multiple copies of the original string
8 - 18
String Methods (7 of 7)
Method Description
endswith(substring) The substring argument is a string. The method returns true if
the string ends with substring.
find(substring) The substring argument is a string. The method returns the
lowest index in the string where substring is found. If
substring is not found, the method returns −1.
replace(old, new) The old and new arguments are both strings. The method returns a
copy of the string with all instances of old replaced by new.
startswith(substring) The substring argument is a string. The method returns true if
the string starts with substring.
8 - 19
Example :
8 - 20
Splitting a String (1 of 2)
• split method: returns a list containing the words in
the string
– By default, uses space as separator
– Can specify a different separator by passing it as an
argument to the split method
8 - 21
Splitting a String (2 of 2)
• Examples:
8 - 22
String Tokens (1 of 4)
• Sometimes a string contains substrings that are
separated by a special character
– Example:
8 - 23
String Tokens (2 of 4)
• Example:
'17;92;81;12;46;5'
– This string contains the tokens 17, 92, 81, 12, 46, and 5
– The delimiter is the ; character
8 - 24
String Tokens (3 of 4)
• Tokenizing is the process of breaking a string into
tokens
• When you tokenize a string, you extract the tokens
and store them as individual items
• In Python you can use the split method to tokenize a
string
8 - 25
String Tokens (4 of 4)
• Examples:
str = 'peach raspberry strawberry vanilla'
tokens = str.split()
print(tokens)
my_address = 'www.example.com'
tokens = my_address.split('.')
print(tokens)
8 - 26
Exercise 1
• Manipulate the following Sentence:
• “I’m doing great with Python”
• To :
– Upper
– Lower
– Count the length
– Swapcase
– Any Three method from Table 5.1
8 - 27
Exercise 2
# Input string input_string = "Hello, world!"
# Calculate the length of the string
length = len(input_string)
# Print the length of the string print("Length of the
string:", length)
8 - 28
Exercise 2
• Create a program that :
– Get Input from User
– Compare the length of the input string by using if – elif
–else
– If it met the condition: then convert the string using a
different string method
8 - 29
Summary
• This chapter covered:
– String operations, including:
Methods for iterating over strings
Repetition and concatenation operators
Strings as immutable objects
Slicing strings and testing strings
String methods
Splitting a string
8 - 30