Python for Network Engineers: String Manipulation

Hello friends, and welcome to another update of my blog series: Python for Network Engineers. Today, we will be learning about String Manipulation in Python. This is an important topic for every aspiring network programmer as majority of the time spent in programming for networks with Python, is handling and manipulating String outputs.

Assigning values to a Python string variable is straightforward: variable_name = “my string here”. In Python, a String is an object that works similarly like an array of characters. What I mean by that is, each character in a string has its own index value which lets you identify each character in a string and perform functions with them. Below is a list of common string methods and how they work.

Length of Characters in a String

More often than not, you’ll want to know how many characters there are in your string. This is achieved by using len() method. Try the script below on your IDLE and the result should be 21, as there are 21 characters in the string “hello wonderful world”.

my_string = 'hello wonderful world'
#returns 21

Not to be confused with count(), which is a method to count how many times a character is found in the string. This method requires a string parameter. Below script returns 2, as the character ‘e’ appears twice (2 times) in the string “hello wonderful world”.

#returns 2

String Lowercase and Uppercase

If you want to change the string into lowercase or uppercase, you can do so by using the methods lower() and upper() respectively. The important thing to note here is that, these methods do not actually change the value. Check out the example in my screenshot below and see that the original value is unchanged.


String startswith() and endswith()

Another handy method is to identify whether a string starts or ends with a specific character. The methods are appropriately named startswith() and endswith(). Both methods return Boolean (True or False).

my_string.startswith('h') #true
my_string.startswith('j') #false
my_string.endswith('e') #false
my_string.endswith('d') #true

String stripping

If you want to remove certain leading and trailing characters from your string, the strip() method is your friend. Without any parameter, the method removes whitespaces and returns your string whitespace-free. You may specify the character you want to remove, in the parameters. In the example below, the method removes all the leading and trailing whitespace. The next example specifies a character to remove.

my_string = '   hello wonderful world ' 
#returns 'hello wonderful world'

new_string = 'hello world'
#returns 'ello world'

It is important to note, that strip only removes leading and trailing characters. If you want to remove characters within the string, like somewhere in the middle, you’ll want to use the method replace(). This method accepts the first parameter as the existing character you want to replace. The second parameter is the new character you want to replace it with. In the example below, we are removing the space between ‘hello’ and ‘world’.

my_string = 'hello world'
my_string.replace(' ','')
#returns 'helloworld'

String Split and Join

Now we will look at how to split a string. But first, why do we want to split a string? In this example, our string has a list of names separated by spaces. We want to be able to use each name, without calling for the entire string, or just a single character. This is where we use the method split().

my_string = 'apple banana orange'
#returns ['apple', 'banana', 'orange']

#you can store this new value into another string
new_string = my_string.split()
#returns banana
#returns apple

#you can specify a delimiter too
my_string = 'london,tokyo,paris'
#returns ['london', 'tokyo', 'paris']

There’s a lot more methods available to use in String handling and manipulation but these are the most common that you will be using in Network Programming. For a list of methods available for Python string, visit this link. I hope you learned something new today friend! In the coming days, we will be learning Python Math operations and Booleans. See you again soon!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s