Strings in Python

Now that you have learned how to work in interactive mode with Python is time to expand your knowledge and experiment with string objects.

Strings are sequences of characters and are coded with single quotes, double quotes or triple quotes. Why do we need strings? Strings are one of the most important data types in computer languages and they are used to keep track of textual information. Since strings are sequences of characters we can perform sequence operations on them, but we can not change the original strings because they are immutable like tuples, which means once defined they cannot be changed. Lists are not immutable, they can be changed. I will write about tuples and lists in the next articles.

Some of sequence operations we can perform on strings that will be explained in this article are: indexing, slicing, concatenation and repetition. These operations can be performed and will work on other sequences like lists and tuples. Strings have their own specific operations and these operations are called methods. For example a specific operation that can work only for strings can be uppercasing string’s letters. Logicaly we create an idea of methods and their purpose. Now, how about finding the length of a string? To find the length of a string we can use the builtin function len(), which is a function that will work on other sequences like list and tuples. Before we continue let me remind you that in order to save objects (strings in our case) and use them later we should assign them to variables; for example string = ‘unixmen’ like shown in Figure 1. We can use single or double quotes, it really doesn’t matter.

Figure 1- Assigning a string to our variable

Now we know from the previous article that when variables are used in expressions they return their actual values. So, if we put the variable string inside len() function like shown in Figure 2 we get the length of our string.

Figure 2 – Finding the length of our string with len() function

The plus sign means different operation on different object types, it means addition for numbers and concatenation for strings. Concatenation means joining two or more strings together, we do this with the plus sign like shown in Figure 3.

Figure 3 – Joining strings with the + sign

The same as with the + sign , the * means repetition for strings and multiplication for numbers. String repetition is shown in Figure 4.

Figure 4 – String Repetition

What is indexing? What about slicing? Indexing is used to fetch sequence components and slicing is a more advanced indexing; slicing is used to extract an entire section of the sequence. An index expression has this form : variable[index]. Index is a number greater or equal than 0 and less or equal than len(variable) -1. This is basic math and you understand that indexing in python starts from 0. The first item in the sequence has the index 0, the second item in the sequence has the index 1 and so on. If i want the first item in the ‘unixmen’ string i use ‘string[0]’ indexing expression where string is the variable assigned to ‘unixmen’ string value.

Figure 5 – Indexing

To perform slicing we use ‘variable[i:j]’ expression. The slice starts from i position including it and stops at j, not including it. Figure 6 shows an example on how to use slicing.

Figure 6 – Slicing