0% found this document useful (0 votes)
59 views

Python 06 Strings

Here are the steps to extract the second half of the email address: 1. Find the position of the '@' symbol using data.find('@') 2. Find the position of the first whitespace after the '@' using data.find(' ', position of @) 3. Slice the string from the position after '@' to the end using data[position after @:] This will extract "uct.ac.za" from the given string.

Uploaded by

Jeanpierre Akl
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Python 06 Strings

Here are the steps to extract the second half of the email address: 1. Find the position of the '@' symbol using data.find('@') 2. Find the position of the first whitespace after the '@' using data.find(' ', position of @) 3. Slice the string from the position after '@' to the end using data[position after @:] This will extract "uct.ac.za" from the given string.

Uploaded by

Jeanpierre Akl
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Strings

Chapter 6
String Data Type
• A string is a sequence of characters

• A string literal uses quotes


'Hello' or "Hello"

• For strings, + means “concatenate”

• When a string contains numbers, it is still a string

• We can convert numbers in a string into a number using int()


Reading and
Converting >>> a = input('Enter:')
Enter:100
>>> x = a – 10
• We prefer to read data in using
Traceback (most recent call
strings and then parse and
last): File "<stdin>", line 1,
convert the data as we need
in <module>
• This gives us more control over TypeError: unsupported operand
type(s) for -: 'str' and 'int'
error situations and/or bad user
>>> x = int(a) – 10
input
>>> print(x)
• Input numbers must be 90
converted from strings
Looking Inside Strings
• We can get at any single character in a
b a n a n a
string using an index specified in square
brackets 0 1 2 3 4 5
>>> fruit = 'banana'
• The index value can be an expression that >>> letter = fruit[1]
is computed >>> print(letter)
a
• You will get a python error if you attempt to >>>
>>>
x = 3
w = fruit[x - 1]
index beyond the end of a string
>>> print(w)
n
Strings Have Length

b a n a n a
The built-in function len gives 0 1 2 3 4 5
us the length of a string
>>> fruit = 'banana'
>>> print(len(fruit))
6
Looping Through Strings
• The iteration variable is
fruit = 'banana'
index = 0
completely taken care of while index < len(fruit) : b
by the for loop letter = fruit[index] a
print(letter)
index = index + 1
n
a
n
• A definite loop using a fruit = 'banana' a
for statement is much for letter in fruit :
more elegant print(letter)
Looping and Counting

word = 'banana'
This is a simple loop that count = 0
loops through each letter in a for letter in word :
string and counts the number if letter == 'a' :
of times the loop encounters count = count + 1
the 'a' character print(count)
Slicing Strings M o n t y P y t h o n
0 1 2 3 4 5 6 7 8 9 10 11
• We can also look at any
continuous section of a string
using a colon operator >>> s = 'Monty Python'
>>> print(s[0:4])
• The second number is one Mont
beyond the end of the slice - >>> print(s[6:7])
“up to but not including” P
• If the second number is >>> print(s[6:20])
beyond the end of the string, Python
it stops at the end
Slicing Strings M o n t y P y t h o n
0 1 2 3 4 5 6 7 8 9 10 11

>>> s = 'Monty Python'


If we leave off the first number >>> print(s[:2])
or the last number of the slice, Mo
it is assumed to be the >>> print(s[8:])
beginning or end of the string
respectively thon
>>> print(s[:])
Monty Python
Strings are
We cannot change an existing
immutable
string! The best you can do is
create a new string with the word = 'banana'
desired changes. word[0] = p
TypeError Traceback (most recent call
last)
word = 'banana'
<ipython-input-22-d8e69a6e748a> in
New_word = 'p‘ + <module>()
word[1:] ----> 1 w[0]=‘p'
TypeError: 'str' object does not
support item assignment
String Concatenation
>>> a = 'Hello'
>>> b = a + 'There'
When the + operator is >>> print(b)
applied to strings, it means HelloThere
“concatenation” >>> c = a + ' ' + 'There'
>>> print(c)
Hello There
>>>
Using in as a Logical Operator
>>> fruit = 'banana'
• The in keyword can also be >>> 'n' in fruit
used to check to see if one True
string is “in” another string >>> 'm' in fruit
False
• The in expression is a >>> 'nan' in fruit
True
logical expression that >>> if 'a' in fruit :
returns True or False and ... print('Found it!')
can be used in an if ...
statement Found it!
>>>
String Comparison
if word == 'banana':
print('All right, bananas.')

if word < 'banana':


print('Your word,' + word + ', comes before banana.')
elif word > 'banana':
print('Your word,' + word + ', comes after banana.')
else:
print('All right, bananas.')
• Python has a number of string String Library
functions which are in the
string library

• These functions are already >>> greet = 'Hello Bob'


built into every string - we >>> zap = greet.lower()
invoke them by appending the >>> print(zap)
function to the string variable hello bob
>>> print(greet)
• These functions do not modify
Hello Bob
the original string, instead they
return a new string that has
been altered
>>> stuff = 'Hello world'
>>> type(stuff)
<class 'str'>
>>> dir(stuff)
['capitalize', 'casefold', 'center', 'count', 'encode',
'endswith', 'expandtabs', 'find', 'format', 'format_map',
'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit',
'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace',
'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip',
'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust',
'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines',
'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper',
'zfill']

https://docs.python.org/3/library/stdtypes.html#string-
methods
Searching a String
b a n a n a
• We use the find() function to search
for a substring within another string
0 1 2 3 4 5

• find() finds the first occurrence of the >>> fruit = 'banana'


substring >>> pos = fruit.find('na')
>>> print(pos)
• If the substring is not found, find() 2
returns -1 >>> aa = fruit.find('z')
>>> print(aa)
• Remember that string position starts -1
at zero
Search and Replace
• The replace() function
is like a “search and >>> greet = 'Hello Bob'
replace” operation in a >>> nstr = greet.replace('Bob','Jane')
>>> print(nstr)
word processor
Hello Jane


>>> nstr = greet.replace('o','X')
It replaces all >>> print(nstr)
occurrences of the HellX BXb
search string with the >>>
replacement string
Stripping Whitespace
• Sometimes we want to take
a string and remove
whitespace at the beginning >>> greet = ' Hello Bob '
and/or end >>> greet.lstrip()
'Hello Bob '

• lstrip() and rstrip() remove


>>> greet.rstrip()
' Hello Bob'
whitespace at the left or right >>> greet.strip()
'Hello Bob'
• strip() removes both >>>
beginning and ending
whitespace
Prefixes
>>> line = 'Please have a nice day'
>>> line.startswith('Please')
True
>>> line.startswith('p')
False
Parsing and Extracting
Assume that we are presented with addresses formatted as below.
How do you proceed to pull out the second half of the strings
(i.e. uct.ac.za)?

From [email protected] Sat Jan 5 09:14:16 2008

Idea: find the @ symbol in the string, and the first whitespace
character after the at-sign. Then use string slicing to extract
the desired portion of the string
Parsing and Extracting
>>> data = 'From [email protected] Sat Jan 5
09:14:16 2008'
>>> startPos = data.find('@')
>>> print(startPos)
21
>>> endPos = data.find(' ', startPos)
>>> print(endPos)
31
>>> host = data[startPos +1 : endPos]
>>> print(host)
uct.ac.za
Format Operator
• The format operator, % allows us to construct strings, replacing
parts of the strings with the data stored in variables.
• We use “%d” to format an integer, “%g” to format a floating point
number, and “%s” to format a string

>>> Items = 32
>>> 'We have %d items' % Items
'We have 32 items‘

>>> ' He bought %d %s last month' % (3, ' cars ')


'We have 32 items'
Summary
• String type • String operations
• Read/Convert • String library
• Indexing strings [] • String comparisons
• Slicing strings [2:4] • Searching in strings
• Looping through strings • Replacing text
with for and while • Stripping white space
• Concatenating strings with +
Exercises
• Write a while loop that starts at the last character in a string and
prints the reversed string.

• Take the following Python code that stores a string:


str = ' X-DSPAM-Confidence:0.8475 '
Use find and string slicing to extract the portion of the string after the
colon character and then use the float function to convert the extracted
string into a floating point number.

You might also like