Introduction
Python is one of the most popular programming languages across the world. It uses a data type that comprises sequences made up of individual characters. These characters may be letters, numbers, symbols, or whitespace characters. Other types of sequence-based data types include lists and tuples. Just like them, you can also access, index, and slice strings in Python.
In this tutorial, we will explore how you can access strings through indexing and slicing them through their sequences in Python 3. We will also touch upon a few methods of counting and character location within strings.
Before We Begin…
Before you begin with this tutorial, there are some prerequisites you should consider. First and foremost, you need to have Python 3 installed on your system or server with a programming environment up and running. You will easily be able to find the program for your particular operating system (Ubuntu, CentOS, Debian, etc).
Indexing Strings
First, we will start by exploring how to index strings. Indexing in Python essentially means listing a data type in a way that each item corresponds to an index number. This means that each character in your string will be associated with an index number. Index numbers always start with 0.
Let’s say we have a string called Sammy Shark!. This is what the index will look like:
Our first character is S which corresponds to the index number 0. Our numbers end at 11 with the ! symbol. You can also see that the space character between the words Sammy and Shark corresponds to its own index number which is 5 in this case. In the end, the symbol ! also has an index number. Other punctuation marks such as *#$&.;? would also have an associated index number.
The benefit of using such indexing in Python is that you can call, access, and manipulate the strings just like other data types.
Using Positive Index Number to Access Characters
Next, we will see how to isolate characters in a string using their index number. For this purpose, you need to put the index numbers in square brackets. We will use the previous example to show you how to declare, print, and call an index number for a string:
1 2 |
ss = "Sammy Shark!" print(ss[4]) |
1 2 |
Output: y |
As you can see, the program brought back the character corresponding to the index number 4 which was y in the case of the string ss = ”Sammy Shark!”.
Using Negative Index Number to Access Characters
Similar to that, you can also use negative index numbers. This means that you are counting backwards from the end of the string instead of the beginning. Continuing with the same example, this is what the negative index breakdown would look like:
You can use these index numbers to print out characters as needed:
1 |
print(ss[-3]) |
1 2 |
Output: r |
This is useful when the string is very long and you need a character at the very end.
How to Slice Strings
Next, we will learn how to slice strings. Say that you only want to print the word
Shark from our sample string. You will do this by creating a slice. Hence, a slice can be defined as a sequence of characters in the original string. To do this, you need to specify a range of index numbers that span your selected slice of the sequence. The numbers are separated with a colon to indicate a range
[x:y]:
1 |
print(ss[6:11]) |
1 2 |
Output: Shark |
It is important to know that the first index number in [6:11] is inclusive. This means that the first index number is where the slice starts. The second index number is where the slice ends, making it exclusive. As such, your second number will be the one that comes after your slice ends. In the process of slicing, the user creates a substring. This is a string that exists in another string. Therefore, ss[6:11] essentially refers to the substring Shark which is present inside the original string Sammy Shark!.
Another scenario is where you may want to include either end of the string. To do so, you simply need to remove the respective number from the formula
string[n:n]. Say you want to print our the word
Sammy:
1 |
print(ss[:5]) |
1 2 |
Output: Sammy |
We simply skipped the first index number in the syntax. We only have to specify the end of the string. Similarly, if you want to call a substring that starts in the middle and stops at the end:
1 |
print(ss[7:]) |
1 2 |
Output: hark! |
We omitted the last index number. Slicing can also be done using the negative index numbers. Remember that negative string numbers start at
-1 and keep counting down from the end to the beginning. Thus, we will put the lower number before the colon as it comes first in the string:
1 |
print(ss[-4:-1]) |
1 2 |
Output: ark |
Python will print out the respective character in the string, which was ark in this case.
What Does it Mean to Specify a Stride?
During slicing, you can add another parameter apart from the index numbers. This is to specify the stride. The stride is the number of characters you want to move forward after the first character. If you do not specify a stride, Python defaults it to 1. This means that every character within the range is called.
Here is an example of printing out the word
Shark:
1 |
print(ss[6:11]) |
1 2 |
Output: Shark |
If you add a stride of 1, you will get the same result:
1 |
print(ss[6:11:1]) |
1 2 |
Output: Shark |
You also have the option to increase the stride. This will make Python skip numbers in the middle. Here is an example with a stride of 2:
1 |
print(ss[0:12:2]) |
1 2 |
Output: SmySak |
A stride of 2 will skip every other character as you can see above. The highlighted characters are basically retrieved:
Sa mm y Sh ar k!One thing of note here is that the whitespace is considered as a character and is also skipped. Here is what happens using a larger stride parameter like 4:
1 |
print(ss[0:12:4]) |
1 2 |
Output: Sya |
Understandably, our substring is smaller since every fourth character is retrieved. You can see that in the following highlighted characters:
Samm y Sh ark!The whitespace is also considered and skipped here. Another way to do this would be to skip the first two index numbers altogether and only specify the stride. This is because we are printing the whole string anyway:
1 |
print(ss[::4]) |
1 2 |
Output: Sya |
Using a negative value for the default stride which is -1 will print the original string in reverse order:
1 |
print(ss[::-1]) |
1 2 |
Output: !krahS ymmaS |
We skipped the two index numbers since we wish to print the full string. Here is the same with a stride of -2:
1 |
print(ss[::-2]) |
1 2 |
Output: !rh ma |
Here, we used the full string but skipped every other character. The negative value made our result reversed:
!k ra hS [whitespace]y mm aSIn this example, the whitespace character is printed.
Counting Methods
Lastly, we will touch upon the counting methods for the strings and characters. This is important for the cases where you want to limit the number of characters that can be accepted in a form, or when you are comparing two or more strings. There are several different counting methods that you can use.
The first method is the
len() method. It will show you the length of any sequence-based data type regardless of whether it is ordered or unordered. Let’s print out the length of the string
ss:
1 |
print(len(ss)) |
1 2 |
Output: 12 |
This accurately shows that the length of the string “Sammy Shark!” is 12 characters. It includes the whitespace as well as the exclamation mark. Another way to do so is to enter a string directly for counting instead of using a variable in its place:
1 |
print(len("Let's print the length of this string.")) |
1 2 |
Output: 38 |
len() shows us the total length of the string. What if we want to know how many times a particular character has shown up in a string? For this purpose, you can use the
str.count() method. Here, we will calculate how many times the character “a” shows up in our string “Sammy Shark!:”
1 |
print(ss.count("a")) |
1 2 |
Output: 2 |
Let’s repeat the same with another character:
1 |
print(ss.count("s")) |
1 2 |
Output: 0 |
Why does the output show 0 when the letter S appears twice in the string? This is because the characters are case-sensitive. Instead, if you would like to search for certain characters regardless of their case, then you can use the str.lower() method. This will convert the whole string to lowercase before counting the characters.
Here is an example of the
str.lower() method:
1 2 |
likes = "Sammy likes to swim in the ocean, likes to spin up servers, and likes to smile." print(likes.count("likes")) |
1 2 |
Output: 3 |
Here, we have asked the program to count the number of times the word
likes appears in the string. The
str.find() method is another interesting feature that allows you to find the position of a character or character sequence in a string based on the index numbers. Let’s find where the character “m” first appears in the string
ss:
1 |
print(ss.find("m")) |
1 2 |
Output: 2 |
This shows that the letter “m” appears first at the number 2 position in the string “Sammy Shark!” which is accurate. Next, let’s try the same function with the word “likes” in the string
likes:
1 |
print(likes.find("likes")) |
1 2 |
Output: 6 |
The output shows that the character sequence “likes” appears at position 6 in the string. This corresponds to the position of the character 1 in the sequence likes.
How about we try to find where the second sequence of “likes” starts. To do this, you need to add a second parameter to the
str.find() method. Adding an index number will allow you to start from that position instead of the start of the string:
1 |
print(likes.find("likes", 9)) |
1 2 |
Output: 34 |
We began counting from the index number of 9. This shows that the first occurrence of the character sequence from this position starts at index number 34. You can also specify an end to this range with an additional parameter if you wish. It can be done with a positive or a negative numeric value:
1 |
print(likes.find("likes", 40, -6)) |
1 2 |
Output: 64 |
Here, we have searched for the sequence “likes” between index number 40 and -6. The negative number indicates we started counting from the end of the string.
Conclusion
As you can see, strings work in a similar fashion to lists and tuples. They can be accessed through indexing and slicing. Thus, using index numbers and slices of strings makes working with sequence-based data types quite feasible in Python. It introduces a lot of flexibility and accessibility to the process.
Finally, here are more resources from our blog that will help you further utilize Python:
- The txt File Format: Working with Plain Text Files in Python 3
- Importing Modules in Python 3: A Comprehensive Guide
- Python 3: A Guide on Converting Data Types
- Using Python 3 String Formatters
- Programming with Python 3: How To Go About Constructing Classes and Defining Objects?
Happy Computing!
- How to Deploy WordPress with Persistent Volume on Kubernetes Cluster - March 17, 2023
- Deploying Applications on Kubernetes Using Argo CD and GitOps - October 26, 2022
- Using Node.js Modules with npm and package.json: A Tutorial - October 6, 2022
- Using Ansible to Install and Configure WordPress with LAMP on Ubuntu - September 23, 2022
- Creating Views in the Django Web Application Framework - September 22, 2022