You are on page 1of 9

10/01/21

String Data Type


>>> str1 = "Hello"
>>> str2 = 'there'
>>> bob = str1 + str2
>>> print(bob)
• A string is a sequence of characters Hellothere
>>> str3 = '123'

Strings • A string literal uses quotes


'Hello' or "Hello"
>>> str3 = str3 + 1
Traceback (most recent call
last): File "<stdin>", line 1,
Chapter 6 • For strings, + means “concatenate” in <module>
TypeError: cannot concatenate
• When a string contains numbers, it is 'str' and 'int' objects
still a string >>> x = int(str3) + 1
>>> print(x)
Python for Everybody 124
• We can convert numbers in a string
www.py4e.com into a number using int()
>>>

1 2

Reading and >>> name = input('Enter:')

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

3 4
10/01/21

A Character Too Far Strings Have Length


• You will get a python error >>> zot = 'abc'
>>> print(zot[5]) b a n a n a
if you attempt to index Traceback (most recent call
beyond the end of a string last): File "<stdin>", line
The built-in function len gives 0 1 2 3 4 5
us the length of a string
• So be careful when
1, in <module>
IndexError: string index out >>> fruit = 'banana'
constructing index values of range >>> print(len(fruit))
and slices >>>
6

5 6

len Function len Function


>>> fruit = 'banana' A function is some stored >>> fruit = 'banana' A function is some stored
>>> x = len(fruit) code that we use. A >>> x = len(fruit) code that we use. A
>>> print(x) function takes some >>> print(x) function takes some
6 input and produces an 6 input and produces an
output. output.
def len(inp):

'banana' len() 6 'banana'


blah
blah 6
for x in y:
(a number) (a number)
(a string) function (a string) blah
blah

7 8
10/01/21

Looping Through Strings Looping Through Strings

Using a while statement, fruit = 'banana' 0b • A definite loop using a b


an iteration variable, and index = 0 1a for statement is much a
fruit = 'banana'
the len function, we can while index < len(fruit): 2n more elegant n
for letter in fruit:
• The iteration variable is
letter = fruit[index] 3a a
construct a loop to look at print(letter)
print(index, letter)
each of the letters in a 4n n
index = index + 1
5a completely taken care of a
string individually by the for loop

9 10

Looping Through Strings Looping and Counting

• A definite loop using a fruit = 'banana'


for letter in fruit :
b This is a simple loop that
word = 'banana'
for statement is much count = 0
print(letter) a loops through each letter in a
more elegant for letter in word :
n string and counts the number if letter == 'a' :
• The iteration variable is index = 0
a
n
of times the loop encounters count = count + 1
completely taken care of while index < len(fruit) : the 'a' character print(count)
letter = fruit[index] a
by the for loop
print(letter)
index = index + 1

11 12
10/01/21

Looking Deeper into in Yes No b a n a n a


Done? Advance letter
• The iteration variable “iterates”
through the sequence Iteration Six-character print(letter)
(ordered set) variable string
• The block (body) of code is
executed once for each value for letter in 'banana' :
for letter in 'banana' :
in the sequence print(letter)
print(letter)
• The iteration variable moves
through all of the values in the The iteration variable “iterates” through the string and the block (body)
sequence of code is executed once for each value in the sequence

13 14

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
More String Operations 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
>>> print(s[6:20])
• If the second number is
Python
beyond the end of the string,
it stops at the end

15 16
10/01/21

Slicing Strings M o n t y P y t h o n String Concatenation


0 1 2 3 4 5 6 7 8 9 10 11
>>> a = 'Hello'
>>> s = 'Monty Python' >>> b = a + 'There'
If we leave off the first number >>> print(s[:2]) When the + operator is >>> print(b)
or the last number of the slice, Mo applied to strings, it means HelloThere
it is assumed to be the >>> print(s[8:]) “concatenation” >>> c = a + ' ' + 'There'
beginning or end of the string thon >>> print(c)
respectively >>> print(s[:]) Hello There
Monty Python >>>

17 18

Using in as a Logical Operator String Comparison


>>> fruit = 'banana'
• The in keyword can also be >>> 'n' in fruit if word == 'banana':
used to check to see if one True print('All right, bananas.')
string is “in” another string >>> 'm' in fruit
False if word < 'banana':
• The in expression is a >>> 'nan' in fruit
True
print('Your word,' + word + ', comes before banana.')
logical expression that elif word > 'banana':
>>> if 'a' in fruit :
returns True or False and print('Your word,' + word + ', comes after banana.')
... print('Found it!')
can be used in an if ... else:
Found it! print('All right, bananas.')
statement
>>>

19 20
10/01/21

>>> stuff = 'Hello world'

• Python has a number of string String Library >>> type(stuff)


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

21 22

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

23 24
10/01/21

Searching a String Making everything UPPER CASE


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 • You can make a copy of a >>> greet = 'Hello Bob'
string in lower case or upper >>> nnn = greet.upper()
• find() finds the first occurrence of the >>> fruit = 'banana' case >>> print(nnn)
substring >>> pos = fruit.find('na')
>>> print(pos) • Often when we are searching HELLO BOB
>>> www = greet.lower()
• If the substring is not found, find() 2 for a string using find() we first
returns -1 >>> aa = fruit.find('z') >>> print(www)
convert the string to lower case
>>> print(aa) hello bob
-1 so we can search a string
• Remember that string position starts >>>
regardless of case
at zero

25 26

Search and Replace Stripping Whitespace


• Sometimes we want to take
• The replace() function a string and remove
is like a “search and >>> greet = 'Hello Bob' whitespace at the beginning >>> greet = ' Hello Bob '
replace” operation in a >>> nstr = greet.replace('Bob','Jane') >>> greet.lstrip()
and/or end
>>> print(nstr) 'Hello Bob '
word processor Hello Jane
• lstrip() and rstrip() remove >>> greet.rstrip()

• It replaces all >>> nstr = greet.replace('o','X')


>>> print(nstr) whitespace at the left or right
' Hello Bob'
>>> greet.strip()
occurrences of the HellX BXb 'Hello Bob'
search string with the >>> • strip() removes both >>>
replacement string beginning and ending
whitespace

27 28
10/01/21

Parsing and
Prefixes Extracting
21 31

>>> line = 'Please have a nice day' From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
>>> line.startswith('Please')
True >>> data = 'From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008'
>>> atpos = data.find('@')
>>> line.startswith('p') >>> print(atpos)
False 21
>>> sppos = data.find(' ',atpos)
>>> print(sppos)
31
>>> host = data[atpos+1 : sppos]
>>> print(host)
uct.ac.za

29 30

Two Kinds of Strings Summary


Python 2.7.10 Python 3.5.1 • String type • String operations
>>> x = '이광춘' >>> x = '이광춘'
>>> type(x) >>> type(x) • Read/Convert • String library
<type 'str'> <class 'str'> • Indexing strings [] • String comparisons
>>> x = u'이광춘' >>> x = u'이광춘'
>>> type(x) >>> type(x) • Slicing strings [2:4] • Searching in strings
<type 'unicode'> <class 'str'> • Replacing text
• Looping through strings
>>> >>>
with for and while • Stripping white space
In Python 3, all strings are Unicode • Concatenating strings with +

31 32
10/01/21

Acknowledgements / Contributions
These slides are Copyright 2010- Charles R. Severance ...
(www.dr-chuck.com) of the University of Michigan School of
Information and open.umich.edu and made available under a
Creative Commons Attribution 4.0 License. Please maintain this
last slide in all copies of the document to comply with the
attribution requirements of the license. If you make a change,
feel free to add your name and organization to the list of
contributors on this page as you republish the materials.

Initial Development: Charles Severance, University of Michigan


School of Information

… Insert new Contributors and Translators here

33

You might also like