We already saw how to define strings:

In [1]:
s = "antidisestablishmentarianism"


(This is the longest word in the English language, according to some)

You can access elements just like in lists, with every letter behaving like an element. The result is a string of length 1.

In [2]:
s[0]

Out[2]:
'a'
In [3]:
s[5]

Out[3]:
'i'

You can use slicing just like with lists, but the result is a string:

In [4]:
s[:4]

Out[4]:
'anti'
In [5]:
s[5:15]

Out[5]:
'isestablis'

You can loop through every letter of the string:

In [6]:
s = "CSC180"
for letter in s:
print(letter)

C
S
C
1
8
0


You can get the length of a string using len()

In [7]:
s = "antidisestablishmentarianism"
len(s)

Out[7]:
28

This means we can also loop through strings using indices:

In [8]:
s = "CSC180"
for i in range(len(s)):
print(s[i])

C
S
C
1
8
0


You can use in to check whether a string is a substring of another string:

In [9]:
s = "antidisestablishmentarianism"
"ism" in s

Out[9]:
True
In [10]:
"a" in s

Out[10]:
True
In [11]:
"z" in s

Out[11]:
False

There are lots of functions available for strings. See here for the full list. One example is str.capitalize(). The value of s.capitalize() is the capitalized version of the string s.

In [12]:
s.capitalize()

Out[12]:
'Antidisestablishmentarianism'

Note that calling str.capitalize() doesn't change the value of s:

In [13]:
s

Out[13]:
'antidisestablishmentarianism'

str.index() works in the same way that list.index() does.

In [14]:
s.index("d")

Out[14]:
4

Like with list.index(), using str.index() to find a substring that's not there results in an error. str.find() is more flexible:

In [15]:
s.find("d")

Out[15]:
4
In [16]:
s.find("ism")

Out[16]:
25
In [17]:
s.find("z")

Out[17]:
-1

s.find(a) returns -1 if it cannot find the index of the subtring a in s.

### Strings are immutable¶

Strings are immutable -- that means that Python does not provide you with the facilities to change their contents. You can, of course, make the string s refer to a new string:

In [18]:
s = "abc"
s = "def" #s now refers to "def"


But then of course you could also make s refer to an int:

In [19]:
s = 42 #s is not even a string anymore


What you cannot do is keep s as the same object, but change its contents: s[0] = "b"

would result in an error.

You can, of course, do something like this:

In [20]:
s = "csc180"
s = s[0] + s[1].capitalize() + s[2:]

In [21]:
print(s)

cSc180


What happenned here is we created a new object by concatenating three strings, and then assigned that new object to s.

Why does it matter? It means that you cannot make a function that modifies the contents of a string:

In [22]:
def capitalize(s):
s = s.capitalize()


Does nothing, since it reassigns a new object to a local variable.

You can, of course, write functions that return a string.