# Lecture 6

2/1/2018

## Strings

A string value is a sequence of characters enclosed in either single-quotes or double quotes.

Examples

a = "cat"
b = "dog"
print(a)
print(b)
print(type(a))
## cat
## dog
## <class 'str'>

Note that strings have their own class.

You can include a single or double quote within a string by using the opposite type to create the string.

s1 = "That's John's book."
s2 = 'She said "Hello there"'
print(s1)
print(s2)
## That's John's book.
## She said "Hello there"

## Concatenation

You can put two strings together by symbolically ‘adding’ them. This is called concatenation.

Example.

fname = 'Joe'
lname = 'Jones'
name = fname + lname
print(name)
## JoeJones

Note that python does not supply a separating blank. You need to supply one if it is required.

fname = 'Joe'
lname = ' Jones'
name = fname + lname
print(name)
## Joe Jones

## Repetition

You can create a string containing multiple copies of a single string by symbolic multiplication.

s1 = 'Hi '
s2 = 5 * s1
s3 = s1 * 5
print(s2)
print(s3)
## Hi Hi Hi Hi Hi
## Hi Hi Hi Hi Hi

Note that there is no difference between these two versions.

## Combinations

You can combine concatenation and repetition using parentheses creatively.

s1 = 'Hi'
s2 = ' '
s3 = 'there'
s4 = 5*(2*(s1+s2)+s3+s2)
print(s4)
## Hi Hi there Hi Hi there Hi Hi there Hi Hi there Hi Hi there

## Indexing

We can refer to individual characters in a string using []. Every character in a string has two possible indices, a positive value and a negative value. Positive indices beging with 0 and start at the beginning of the string and negative values begin with -1 and start at the end of the string.

s ="Hi there"
f = s[0]
print(f)
l = s[-1]
print(l)

f2 = s[-8]
print(f2)

l2 = s[7]
print(l2)
## H
## e
## H
## e

## Exercise

What are the two indices of ‘t’ in the string ‘Hi there’?

s ="Hi there"
ipos = 3
print(s[3])

ineg = -5
print(s[ineg])
## t
## t

## A Relationship

There is a function len(), which gives the length of a string. Note that in the example above that ipos - ineg = len(s) if ipos and ineg refer to the same position in s.

s ="Hi there"
ipos = 3
print(s[3])

ineg = -5
print(s[ineg])

print(ipos - ineg)
print(len(s))
## t
## t
## 8
## 8

## Playing with the Relationship

$ipos - ineg = len(s)$ Solve for ipos and we get

$ipos = ineg + len(s)$ Solve for ineg and we get

$ineg = ipos - len(s)$

## Slicing

We can use [:] to get longer parts of strings. Normally there are integers before and after the ‘:’. I think of these as ‘include’ and ‘exclude’.For example s[0:3] would begin with s[0] and end with s[2].

Here are some examples

s = 'abcdefghi'
print(s[0:3])

print(s[1:2])

print(s[3:5])
## abc
## b
## de

The three points to keep in mind are:

• Python indices start from 0, not 1.
• In a slice, the start is included.
• In a slice, the end is excluded.

## Exercise

Using the slice operator and the string above, print the strings ‘fgh’ and ‘bcdefg’.

s = 'abcdefghi'
print(s[5:8])
print(s[1:7])
## fgh
## bcdefg

## Missing Start or End

If either start or end in [start:end] is missing, the slice goes all the way to that end of the string.

s = 'abcdefghi'
print(s[:3])
print(s[3:])
## abc
## defghi

## Relational Operators

The operators in and not in can be placed between two strings to create a boolean value with obvious meanings.

s = 'abcdefghi'

print('ab' in s)
print('ac' in s)
print('ab' not in s)
print('ac' not in s)
## True
## False
## False
## True

## Exercise

Consider the name Alex Smith. Write python code to extract the first name into a string named fname and print it.

s = 'Alex Smith'
fname = s[0:4]

## Exercise

Using the slice operator, create the last name in a variable lname and print it.

s = 'Alex Smith'
lname = s[5:]
print(lname)
## Smith

## Exercise

Write python code to print the name above in the form ‘Smith, Alex’.

s = 'Alex Smith'
print(s2)
## Smith, Alex