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

Chapter 4. Strings

This document summarizes key aspects of strings in Python. It discusses the string data type, indexing and slicing strings, looping through strings, concatenating strings with +, using the in operator to check for substrings, comparing strings, and functions in the string library. The document provides examples of common string operations like accessing characters, counting occurrences, and formatting strings.

Uploaded by

Anh Nhật Bùi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Chapter 4. Strings

This document summarizes key aspects of strings in Python. It discusses the string data type, indexing and slicing strings, looping through strings, concatenating strings with +, using the in operator to check for substrings, comparing strings, and functions in the string library. The document provides examples of common string operations like accessing characters, counting occurrences, and formatting strings.

Uploaded by

Anh Nhật Bùi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Chapter 4: Strings

Contents
• String type
• Indexing strings []
• Slicing strings [2:4]
• Looping through strings with for and while
• Concatenating strings with +
• in as an operator
• String comparison
• String library (Searching and Replacing text, Stripping white
space )

2
String Data Type
• A string is a sequence of >>> str1 = "Hello“
characters >>> str2 = 'there‘
>>> bob = str1 + str2
• A string literal uses quotes >>> print(bob)
‘Hello’ or “Hello” Hellothere
>>> str3 = '123‘
• For strings, + means >>> str3 = str3 + 1
“concatenate” Traceback (most recent call
last): File "<stdin>", li
• When a string contains ne 1, in <module>TypeError:
numbers, it is still a string cannot concatenate 'str' a
nd 'int' objects
• We can convert numbers in >>> x = int(str3) + 1
a string into a number using >>> print(x)
int() 124

3
Handling User Input
• We prefer to read >>> name = raw_input('Enter:')
Enter:Chuck
data in using strings >>> print(name)
and then parse and Chuck
convert the data as we >>> apple = raw_input('Enter:')
need Enter:100
>>> x = apple – 10
• This gives us more Traceback (most recent call las
control over error t): File "<stdin>", line 1, in
situations and/or bad <module>TypeError: unsupported
user input operand type(s) for -
: 'str' and 'int‘
• Raw input numbers >>> x = int(apple) – 10
must be converted >>> print(x)
from strings 90

4
Looking Inside Strings
• We can get at any single b a n a n a
character in a string using 0 1 2 3 4 5
an index specified in square
brackets
>>> fruit = 'banana‘
• The index value must be an
>>> letter = fruit[1]
integer and starts at zero
>>> print(letter)
• The index value can be an a
expression that is computed >>> n = 3
>>> w = fruit[n - 1]
>>> print(w)
n

5
A Character Too Far
• You will get a python error if you attempt to index beyond
the end of a string.
• So be careful when constructing index values and slices

>>> zot = 'abc‘


>>> print(zot[5])
Traceback (most recent call last): File "<stdin>", line
1, in <module>IndexError: string index out of range
>>>

6
Strings Have Length
• There is a built-in function len that gives us the length of a
string

b a n a n a
0 1 2 3 4 5

>>> fruit = 'banana'


>>> print(len(fruit) )
6

7
len Function
• A function is some stored code that we use. A function takes
some input and produces an output.
>>> fruit = 'banana‘
>>> x = len(fruit)
>>> print(x)
6

'banana' len() 6
(a string) function (a number)

8
Looping Through Strings
• Using a while statement and an iteration variable, and the
len function, we can construct a loop to look at each of the
letters in a string individually

fruit = 'banana' 0b
index = 0 1a
while index < len(fruit): 2n
letter = fruit[index] 3a
print(index, letter) 4n
index = index + 1 5a

9
Looping Through Strings using a “for”
statement
• A definite loop using a for statement is much more elegant
• The iteration variable is completely taken care of by the for
loop

fruit = 'banana' b
for letter in fruit: a
n
print(letter)
a
n
a

10
Looping and Counting
• This is a simple loop that loops through each letter
in a string and counts the number of times the loop
encounters the 'a' character.

word = 'banana'
count = 0
for letter in word:
if letter == 'a':
count = count + 1
print(count)

11
Looking deeper into in
• The iteration variable “iterates” though the sequence
(ordered set)
• The block (body) of code is executed once for each value in
the sequence
• The iteration variable moves through all of the values in the
sequence

Six-character string
Iteration variable

for letter in 'banana':


print(letter)

12
Looking deeper into in
• The iteration variable “iterates” though the string and the
block (body) of code is executed once for each value in the
sequence

b a n a n a

letter

for letter in 'banana':


print(letter)

13
Slicing Strings
• We can also look at
any continuous M o n t y P y t h o n
section of a string 0 1 2 3 4 5 6 7 8 9 10 11
using a colon
operator
>>> s = 'Monty Python'
• The second number
is one beyond the >>> print(s[0:4])
end of the slice - Mont
“up to but not >>> print ( s[6:7])
including”
P
• If the second >>> print(s[6:20])
number is beyond
the end of the Python
string, it stops at
the end

14
Slicing Strings
• If we leave off
the first M o n t y P y t h o n
number or the 0 1 2 3 4 5 6 7 8 9 10 11
last number of
the slice, it is >>> s = 'Monty Python'
assumed to be >>> print(s[:2])
the beginning Mo
or end of the >>> print(s[8:])
string thon
respectively >>> print(s[:])
Monty Python

15
String Concatenation
• When the + operator is applied to strings, it means
"concatenation"

>>> a = 'Hello'
>>> b = a + 'There'
>>> print(b)
HelloThere

>>> c = a + ' ' + 'There'


>>> print(c)
Hello There

16
The String Formatting Operator: %
• Used for math when the operand on the left is a number
the % is the modulus operator
• However when the operand to the left of the % operator is a
string then % is the string format operator.

>>> 32 % 5
2

>>> b = “Gold”
>>> print(“%s is a metal” % b)
Gold is a metal

17
The String Format Operator: Dissected

format string

s = “%s is a metal” % b
string formatting code String formatting operator

18
• The string format operator with more than one value being
inserted into the format string

b = “platinum”
a = 5
s = “%s is one of %d shiny metals” % (b, a)
print(s)
platinum is one of 5 shiny metals

19
String Formatting Codes
%s String
%c Character
%d Decimal (int)
%i Integer
%f Float
* Note: there are others, these are the most common ones.

20
String Formatting Codes Advanced Usage

field width = 6

%-6.2f type of formatting = float

string format code symbol number of decimal places = 2

left justify

21
Using in as an Operator
• The in keyword can >>> fruit = 'banana'
also be used to >>> 'n' in fruit
check to see if one True
string is "in" another >>> 'm' in fruit
string False
>>> 'nan' in fruit
• The in expression is True
a logical expression >>> if 'a' in fruit :
and returns True or print('Found it!')
False and can be Found it!
used in an if
statement

22
String Comparison
word = 'blueberry'

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.')

23
String Library
• Python has a number of >>> greet = 'Hello Bob'
string functions which are >>> zap = greet.lower()
in the string library >>> print(zap)
• These functions are hello bob
already built into every >>> print(greet)
string - we invoke them Hello Bob
by appending the >>> print('Hi There'.lo
function to the string
variable wer())
hi there
• These functions do not >>>
modify the original string,
instead they return a new
string that has been
altered

24
The Directory Function – dir()
>>> stuff = 'Hello world'
>>> type(stuff)
<type 'str'>
>>>> dir(stuff)
['capitalize', 'center', 'count', 'decode', 'enco
de', 'endswith’,'expandtabs', 'find’,'format’,
'index','isalnum','isalpha','isdigit’, 'islower',
'isspace', 'istitle', 'isupper', 'join’,
'ljust','lower', 'lstrip', 'partition','replace',
'rfind', 'rindex', 'rjust', 'rpartition’,
'rsplit', 'rstrip', 'split', 'splitlines', 'start
swith', 'strip', 'swapcase', 'title’,'translate',
'upper', 'zfill']

25
String Library
str.capitalize()
str.center(width[, fillchar])
str.endswith(suffix[, start[, end]])
str.find(sub[, start[, end]])
str.lstrip([chars])
str.join(x [, sep])
str.replace(old, new[, count])
str.lower()
str.rstrip([chars])
str.strip([chars])
str.upper()

26
Searching a String
• We use the find()
function to search for a b a n a n a
substring within 0 1 2 3 4 5
another string
• find() finds the first >>> fruit = 'banana'
occurance of the >>> pos = fruit.find('na')
substring >>> print(pos)
2
• If the substring is not
found, find() returns -1 >>> aa = fruit.find('z')
• Remember that string >>> print(aa)
position starts at zero -1

27
Making everything UPPER CASE
• You can make a copy of a string in lower case or upper case
• Often when we are searching for a string using find(), we
first convert the string to lower case so we can search a
string regardless of case

>>> greet = 'Hello Bob'


>>> nnn = greet.upper()
>>> print(nnn)
HELLO BOB

>>> www = greet.lower()


>>> print(www)
hello bob
>>>
28
Search and Replace
• The replace() function is like a “search and replace”
operation in a word processor
• It replaces all occurrences of the search string with the
replacement string
>>> greet = 'Hello Bob'
>>> nstr = greet.replace('Bob','
Jane')
>>> print(nstr)
Hello Jane

>>> greet = 'Hello Bob'


>>> nstr = greet.replace('o','X'
)
>>> print(nstr)
HellX BXb
>>>

29
Stripping Whitespace
• Sometimes we want to >>> greet = ' Hello Bob '
>>> greet.lstrip()
take a string and 'Hello Bob '
remove whitespace at
the beginning and/or >>> greet.rstrip()
end ' Hello Bob'

• lstrip() and rstrip() to >>> greet.strip()


the left and right only 'Hello Bob'
>>>
• strip() Removes both
beginning and ending
whitespace

30
Prefixes
>>> line = 'Please have a nice day'
>>> line.startswith('Please')
True

>>> line.startswith('p')
False

31
21 31

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

>>> data = 'From [email protected] Sat Jan 5 09:14:16 2008'


>>> atpos = data.find('@')
>>> print(atpos)
21
>>> sppos = data.find(' ',atpos)
>>> print(sppos)
31
>>> host = data[atpos + 1: sppos]
>>> print(host)
uct.ac.za

32
References
1. MIT Introduction to Computer Science and Programming
in Python
2. Think Python: How to Think Like a Computer Scientist:
https://greenteapress.com/thinkpython2/html/index.html

33
Thank you for
your attention!

34

You might also like