This post is part of an ongoing series - see all the posts below.
In the last part, we introduced the basic data types in Python, strings, integers and floats. We saw what they are and how to assign them to variables, but we did not go very deep into what you can actually do with them. In this chapter we will slow down and look at each one properly.
Strings
We briefly looked at strings in the last chapter when we stored text values like device names and IP addresses. Strings are one of the most common data types you will work with in network automation because almost everything that comes back from a network device, command output, hostnames, interface names, IP addresses, is text.
A string is just a sequence of characters wrapped in quotes. You can use single quotes or double quotes, both work the same way.
hostname = "R1_LND_DC"
location = 'London'
The only time it matters which quote you pick is when the string itself contains a quote. If your string has a single quote inside it, wrap it in double quotes, and vice versa.
message = "It's a Cisco device"
description = 'The interface is "up/up"'
String Methods
A method is something you can run on a value to do something useful with it. You call a method by adding a dot after the value (or the variable holding it), followed by the method name and parentheses. Strings come with a lot of built-in methods, and a few of them are very handy for network automation.
hostname = "r1_lnd_dc"
print(hostname.upper())
print(hostname.lower())
print(hostname.capitalize())
R1_LND_DC
r1_lnd_dc
R1_lnd_dc
.upper() converts the string to all uppercase, .lower() to all lowercase, and .capitalize() makes only the first letter uppercase. These are useful when you want consistent formatting, for example, when comparing hostnames or interface names where the case might be different.
Another method you will use a lot is .strip(). It removes any whitespace (spaces, tabs, new lines) from the beginning and end of a string. This is very common when parsing output from network devices because there is often extra whitespace you do not want.
raw_output = " GigabitEthernet0/1 \n"
clean_output = raw_output.strip()
print(f"Before: '{raw_output}'")
print(f"After: '{clean_output}'")
Before: ' GigabitEthernet0/1
'
After: 'GigabitEthernet0/1'
Notice the difference in the output. Before the strip, the value has extra spaces around it and a newline at the end (which is why the closing quote drops to the next line). After .strip(), all that extra whitespace is gone and we are left with just the interface name. This is really useful when parsing command output from devices, since the output often has stray spaces and newlines that would break comparisons or string matching later on.
The .replace() method lets you replace one part of a string with another. It takes two arguments, the text you want to find and the text you want to replace it with.
interface = "GigabitEthernet0/1"
short_name = interface.replace("GigabitEthernet", "Gi")
print(short_name)
Gi0/1
Quick note on that word "arguments" When you call a method or function and put values inside the parentheses, like "GigabitEthernet" and "Gi" above, those values are called arguments. You are passing information into the method so it has something to work with. Some methods take one argument, some take two like .replace(), and some take none at all. We will get into functions and arguments properly in a later chapter, for now just know that whatever goes inside those parentheses is how you feed data into a method.
Next, the .split() method takes a string and breaks it apart into a list based on a separator. By default, it splits on whitespace.
line = "Et0/1 connected 1 full auto"
parts = line.split()
print(parts)
['Et0/1', 'connected', '1', 'full', 'auto']
You can also tell .split() to use a specific character as the separator. For example, if you have an IP address and you want to break it apart by the dots.
ip_address = "192.168.1.1"
octets = ip_address.split(".")
print(octets)
['192', '168', '1', '1']
You will notice the output here uses square brackets with commas separating the values. That is called a list, which is Python's way of storing multiple values in order. Lists are one of the data types we mentioned at the start of this chapter, and we will cover them properly in a later chapter. For now, just know that .split() gives you back a list, and each item can be accessed or looped through later on.
String Indexing and Slicing
Every character in a string has a position, called an index. Python starts counting from zero, so the first character is at index 0, the second is at index 1, and so on.
hostname = "R1_LND_DC"
print(hostname[0])
print(hostname[1])
print(hostname[3])
R
1
L
The [0] part is called indexing. You put square brackets right after the string, and inside them a number that tells Python which character position you want. hostname[0] says "give me the character at index 0 of hostname." Change the number and you get a different character, hostname[3] gives you the character at index 3, which is L.
You can also count backwards from the end using negative numbers. -1 gives you the last character, -2 gives you the second to last, and so on.
hostname = "R1_LND_DC"
print(hostname[-1])
print(hostname[-2])
C
D
Slicing lets you grab a portion of the string instead of just one character. The syntax is string[start:end], where start is included and end is not.
hostname = "R1_LND_DC"
print(hostname[0:2])
print(hostname[3:6])
R1
LND
If you leave out the start, Python assumes you mean the beginning. If you leave out the end, Python assumes you mean all the way to the end.
hostname = "R1_LND_DC"
print(hostname[:2])
print(hostname[3:])
R1
LND_DC
Checking What is Inside a String
You can check if a smaller string exists inside a bigger string using the in keyword. This returns either True or False.
output = "Interface GigabitEthernet0/1 is up, line protocol is up"
print("up" in output)
print("down" in output)
True
False
This is very useful when you run a command on a device and want to check the result. For example, you can check if an interface is up or if a specific configuration line exists in the running config.
Quick note on that True and False you're seeing. Those are booleans, a data type that can only ever be one of those two values. When you ask Python "is this inside that string," it checks and gives you back a boolean answer, True if it found it, False if it did not. We will cover booleans properly in the next section, for now just know that in always hands you back one of those two values.
String Length
The len() function tells you how many characters are in a string. Note that len() is a function, not a method, so the syntax is slightly different. You pass the string as an argument instead of calling it with a dot.
hostname = "R1_LND_DC"
print(len(hostname))
9
You will remember that earlier in this chapter, we used things likehostname.upper()andhostname.strip(). Those are methods, and you call them by putting a dot after the value, followed by the method name and parentheses.len(), on the other hand, is a function, and you call it by writing the name first and passing the value inside the parentheses. We will cover both properly in a later chapter, but for now, just know the difference in how they look.

