Python Strings

Python Strings (type str) is the next Python data type we will explore.  Python strings are any data that is wrapped in a single (‘ ‘) or double (“ “) quotes. Strings are an ordered set of characters. You can retrieve elements from their index. You can do this by putting an [] at the end of the string or variable to get the index(es) you want. Strings are immutable, meaning that strings, once assigned to a variable, can not change. Once assigned, the only way to change a Python string is to assign the change to a new variable.

Here are some examples of strings

'the cat is red'
'12345'
"The dog likes to go for a walk"
'abcd1234'
'a'
"What's your name?"

Strings are limited to the text being on one line.  If you try to write a multi-line string with single or double quotes,

'This is a multi
line string'

This will cause an error; the script will stop running and exit immediately. The error thrown below is a SyntaxError: EOL while scanning string literal.

File "", line 1 'This is a multi ^ SyntaxError: EOL while scanning string literal

The error may look intimidating for a first-time Python user, breaking it down;

  • SyntaxError:
    • This is an error that points out there is an issue with your syntax.
    • It tells you a few lines up what line the error is on
  • EOL while scanning string literal
    • This tells you it can not find the end of the line; your single or double quote is missing.

Multi-Line Python Strings

We can do multi-line Python Strings. Multi-line strings are written with three single quotes in succession:

'''This is how we do a multi
line string in Python!
Cool, isn't it?'''

Python String Escape Sequences

The statement was not printed, so we see the raw return of the string. The \\\\n’s are escape sequences that have special meaning in Python; the \n escape sequence signifies a new line when printed:

print('''This is how we do a multi
line string in Python!
Cool, isn't it?''')

We do not see the \n escape sequence as we do with the raw output.  In this instance, the \n prints a new line when passing through the print function.

This is how we do a multi line
string in Python!
Cool, isn't it?

Some other helpful escape sequences are:

  • \t
    • Insert a tab (4 white spaces)
  • \r
    • carriage return
  • If you are printing and you use single quotes (‘ ‘), and if you are printing text with an apostrophe, you need to escape it:
    • print('Don't touch the wet paint')
    • Another way if you do not want to escape the apostrophe is to use double quotes (” “)

Python String Indexing and Slicing

For string slicing and indexing, we will use this sample string,

'Routed Interface'

String Indexing

If we wanted to pull the first letter of the string,

'Routed Interface'[1]
'o'

This does not seem right; why is the first index returning the o and not the R?

String indexes do not start at index 1; they start at index 0,

'Routed Interface'[0]
'R'

Reverse indexing can be done on the string by specifying the index as a negative.  So, if we wanted the 4th letter from the last:

'Routed Interface'[-4]
'f'

String Slicing

Along with indexing, there is a concept called slicing that you can do with ordered data sets. Slicing lets you grab ranges of characters from the string. The syntax is: [start:stop:step] and the expected input can include the following index locations:

  • Start
    • At what index do you want to start at
  • End
    • At what index do you want to end at
    • This is up to, not including
  • Step
    • What step size do you want

Let us change our string to the alphabet if we wanted from the letter G to the end:

'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[6:]
'GHIJKLMNOPQRSTUVWXYZ'

What if we wanted to start from C up to and including P?

'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[2:16]
'CDEFGHIJKLMNOP'

Our stop index is 16, and 16 is the letter Q.  Why did it only print up to P?  The stop index is up to, not including.

Let us look at some more with stop.

Grab every even letter up to M, skipping every 2nd letter

'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[:14:2]
'ACEGIKM'

Grab every 5th letter

'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[::5]
'AFKPUZ'

A neat trick with slicing and indexing is that we can return the string in reverse.

'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[::-1]
'ZYXWVUTSRQPONMLKJIHGFEDCBA'

What we are doing in this slice is starting at the beginning and going all the way to the end, as the start and stop indexes are empty. Our step is -1, which will tell Python to count backward every position.

If you only wanted to count backward to M:

'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[:11:-1]
'ZYXWVUTSRQPONM'

https://docs.python.org/3/library/string.html