Strings - Continued#

Definition#

To repeat, a string is a sequence (i.e., it is ordered) of characters surrounded by either single, double, or even triple quotes.

'I am a string!'

# or

"I am a string!"

# or even

'''
You can use these 
triple quotes
for multiline strings
    and even literal formatting
 '''

Just remember, however, that whatever kind of quotation mark starts the string, it must also end the string. That is, you cannot start a string with " and end with ' and vice versa.

When you use which is not always important, but sometimes what you might do within the string helps drive your decision. For example, double quotes "" allows you to include apostrophes (e.g., for contractions or possessives) inside of quotes as a character in the string.

"I'm glad to know ya."

In the case of that contraction above, were you to have started the string with single quotes, you would have had to escape the apostrophe:

mystring = 'I\'m glad to know ya.'
print(mystring)
I'm glad to know ya.

(The backslash \ just before the apostrophe ‘escapes’ it, i.e., it is read literally and not interpreted as the end of the string.)

Variables Are Not Strings#

Sometimes you might think you are creating a string, but you forget the quotation marks. Hence, if you were to run:

hello

you’ll get an error — NameError: name 'Hello' is not defined

The NameError typically means you used a variable-like term, but you’ve used it before you defined it.

Consider which of the following are valid strings in Python and try them out in a code cell:

a. "Burger"

b. 'Burger"

c. "Burger

d. Burger

e. '"Burger'

Using Operators on Strings#

We can also use the plus operator + on strings, but with a different opoeration than on numbers.

With string, plus + means concatenation, i.e., chaining strings together.

"Fred" + "Wilma" outputs the concatenation of the two strings as FredWilma.

fred = "Fred"
wilma = "Wilma"

fred + wilma
'FredWilma'

You can create a space between the strings by adding a space to one of the strings. You can also continue to add strings as many times as you need.

name = 'Betty'
print('Hello ' + name + '!' + '!' + '!')
Hello Betty!!!

Type Matters When Using + Operator#

You cannot concatenate a string with a number type:

>>> "Hello " + 2023
...
TypeError: can only concatenate str (not "int") to str

Thus, you need to convert the integer to a string:

>>> "Hello " + str(2023)
'Hello 2023'

But You Can Multiply Strings with * and Integers#

>>> "Hello" * 3
'HelloHelloHello'

Indexing Strings#

Because strings are sequences, they are ordered — meaning you can retrieve them by indexes and slices.

As noted earlier, when you want to select sub-sequences use the square brackets [] to specify which part of the string you want to select.

some string[<integer value>]

remembering that sequences are indexed beginning at zero:

sentence = "The quick brown fox jumps over the lazy dog"

print(sentence[0]) # prints 'T'
T

Another way to think of this is comparing a string to a series of vectors, i.e., each character in the string is a 2-dimensional vector containing both an index position and a character value.

Once again here is the index mapping:

index map

Beware of the index range. If you choose a index value that doesn’t exist, Python will crash the program:

>>> name = 'Yuri'
>>> print name[4]
IndexError: string index out of range

Quiz Yourself#

Given the variable


s = 'elephant'

which of these pairs are two things with the exact same value?

a. s[3] <-> s[1+1+1]

b. s[0] <-> (s+s)[0]

c. s[0] + s[1] <-> s[0+1]

d. s[1] <-> (s + 'aei') [1]

e. s[-1] <-> (s + s)[-1]

Selecting Sub-Sequences from Strings#

"some string"[integer] --> a one-character string

"some string"[integer:another integer] --> a substring of the string

For example:

word = 'assume'

print (word[3])
# u
print (word[4:6])
# me
print (word[4:])
# me
print (word[:2])
# as
print (word[:])
# assume
u
me
me
as
assume