Pythonlearn Strings
Pythonlearn Strings
Chapter 6
String Data Type
>>> str1 = "Hello"
>>> str2 = 'there'
>>> bob = str1 + str2
>>> print(bob)
• A string is a sequence of characters Hellothere
>>> str3 = '123'
• A string literal uses quotes >>> str3 = str3 + 1
'Hello' or "Hello" Traceback (most recent call
last): File "<stdin>", line 1,
• 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)
• We can convert numbers in a string 124
>>>
into a number using int()
Reading and >>> name = input('Enter:')
Enter:Chuck
Converting >>> print(name)
Chuck
>>> apple = input('Enter:')
• We prefer to read data in using
Enter:100
strings and then parse and
>>> x = apple – 10
convert the data as we need
Traceback (most recent call
• This gives us more control over last): File "<stdin>", line 1,
in <module>
error situations and/or bad user
TypeError: unsupported operand
input
type(s) for -: 'str' and 'int'
• Input numbers must be >>> x = int(apple) – 10
>>> print(x)
converted from strings
90
Looking Inside Strings
• We can get at any single character in a b a n a n a
string using an index specified in 0 1 2 3 4 5
square brackets
>>> fruit = 'banana'
• The index value must be an integer >>>
>>>
letter = fruit[1]
print(letter)
and starts at zero a
>>> x = 3
• The index value can be an expression >>> w = fruit[x - 1]
that is computed >>> print(w)
n
A Character Too Far
• You will get a python error >>> zot = 'abc'
>>> print(zot[5])
if you attempt to index
Traceback (most recent call
beyond the end of a string last): File "<stdin>", line
1, in <module>
• So be careful when IndexError: string index out
constructing index values of range
and slices >>>
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
len Function
>>> fruit = 'banana' A function is some stored
>>> x = len(fruit) code that we use. A
>>> print(x) function takes some
6 input and produces an
output.
'banana' len() 6
(a number)
(a string) function
len Function
>>> fruit = 'banana' A function is some stored
>>> x = len(fruit) code that we use. A
>>> print(x) function takes some
6 input and produces an
output.
def len(inp):
blah
'banana' blah 6
(a string) for x in y:
blah
(a number)
blah
Looping Through Strings
print(letter)
The iteration variable “iterates” through the string and the block (body)
of code is executed once for each value in the sequence
More String Operations
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
https://docs.python.org/3/library/stdtypes.html#string-methods
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()
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