Python Strings

and is one of the Python data types.

Double or Single Quotes

You can use either double or single quotes. For example:

>>> "hello world"
'hello world'
>>> 'hello world'
'hello world'

Both work. What’s the difference? You could pick one of the two when you want to use a single or double quote within the string. Let me show you an example:

>>> 'This is the output of my router's show running-config command'
SyntaxError: invalid syntax

We get an invalid syntax error. Why? Python thinks the string ends with the single quote behind the word “router”. We can work around this by using double quotes for our string:

>>> "This is the output of my router's show command"
"This is the output of my router's show command"

This also applies the other way around. For example:

>>> "My router is the "best" there is"
SyntaxError: invalid syntax

When we switch to single quotes, we can use double quotes within our string:

>>> 'My router is the "best" there is'
'My router is the "best" there is'

This string is now valid.

Escape character

Escaping a single or double quote within a string is possible with the examples above but there is a better option. We can use the \ (backslash) as an escape character. Here are two examples:

>>> 'My router is the \'best\' there is'
"My router is the 'best' there is"
>>> "My router is the \"best\" there is"
'My router is the "best" there is'

By using a \ before the character we want to escape, Python ignores it and correctly prints the string.

Triple Quote Multiline String

If you want a string that consists of more than one line, we can use triple quotes. Here is an example:

>>> """This is the output of R1:
Interface GigabitEthernet0/1
 IP address 192.168.1.1 255.255.255.0"""
'This is the output of R1:\nInterface GigabitEthernet0/1\n IP address 192.168.1.1 255.255.255.0'

The output above looks clunky. Between the lines, you can see the \n symbol. Python interprets this as a new line. If you want the string to look nice, you should use the print command. For example:

>>> print("""This is the output of R1:
Interface GigabitEthernet0/1
 IP address 192.168.1.1 255.255.255.0""")
This is the output of R1:
Interface GigabitEthernet0/1
 IP address 192.168.1.1 255.255.255.0

Because of the print command, Python processes the /n newline symbol and prints the second part of the string on a new line.

String Concatenation

In Python, we can concatenate (combine) strings with the + symbol. Here is a quick example:

>>> "Router " + "R1"
'Router R1'

This combines “Router ” and “R1” into “Router R1”.

You can also multiple strings:

>>> "Error " * 3
'Error Error Error '

This shows the string multiple times.

Concatenation Errors

You can’t concatenate everything. For example:

>>> "Error Code: " + 3
Traceback (most recent call last):
  File "", line 1, in 
    "Error Code: " + 3
TypeError: can only concatenate str (not "int") to str

Python tells us that we can’t concatenate a string and an integer. Hmm, too bad. There are two ways to work around this. The first option is to represent our integer as a string:

>>> "Error Code: " + "3"
'Error Code: 3'

This works, but it’s not always possible. What if the error code is returned from an external program which reports it as an integer?

Fortunately, Python can convert between different data types. We can fix this by converting our integer into a string:

>>> "Error Code: " + str(3)
'Error Code: 3'

The str() function lets us convert something into a string. Problem solved!

String Formatting

We can use string formatting to create a new string. Let me show you a quick example. First, we create a new variable with a string:

>>> ip_address = "192.168.1.1"

Now we use the above variable with string formatting:

>>> "We will connect to: %s" % ip_address
'We will connect to: 192.168.1.1'

Here’s how it works:

  • The %s within our string is a substitute.
  • The % operator at the end of the string formats a variable we specify with our string.

Ask a question or start a discussion by visiting our Community Forum