What Is String Indexing in Python?
String indexing in Python means to use the nature of the string data type to manipulate and search strings by index. Python strings are strings of "characters," and each character resides at an index, starting at 0 (for the first character) and ending at string length minus 1 (for the last character). String objects contain methods to use these indexes to navigate the characters, search for values, return substrings, and a variety of other operations.
Remember that strings are immutable, meaning that a programmer may search for patterns in strings, but not directly add or remove items from strings.
-
Strings and Indexes
-
In other programming languages such as C/c++, data structures exist called "arrays" that hold collections of data. In these a programmer could assign and retrieve data based on its index in the array. Furthermore, there was no data type to represent a string of characters. Instead, programmers had to create arrays of characters, which would represent the string. Because the strings were just arrays, each character was accessible by index. In this example of C code, a programmer creates a character array and accesses a character by index:
#include <stdio.h>
#include <string.h>
int main(){
char strng[30] = "Hello There";
printf("%c", strng[1]); //prints the character "e"
return 0;
}
Searching Strings
-
Python has its own string data type, and programmers can declare strings just as any other variable. A programmer does not need to set aside any array-type data structure, just declare a variable and assign a string to it. However, the ability to use indices to locate characters in a string remains, and serves as cornerstone for string manipulation. As in this example, a Python programmer can assign a string to a variable, and then print out characters based on index:
>>>strng = "Hello There"
>>>strng[6]
'T'
Searching Strings
-
Because of these indexing capabilities, the string data type has numerous built-in functions that use indexes to perform operations from the string. A typical operation is searching a string for characters or substrings. In practice, a programmer can accomplish this through a looping structure that checks each character in a string. However, the string class includes a "find()" function that finds the location of character and returns its index, or finds the first instance of a substring --or smaller string inside a larger string -- and returns its starting index, as in this example:
>>>strng.find('l')
2
>>>strng.find('Th')
6
Patterns
-
Because string functions can use indexes to check for patterns, this functionality allows all sorts of generic functions to exist in the string library. The "endswith()" checks a string or substring to see if it ends with a user-supplied pattern. The "partition()" function finds the first occurence of a character or substring and splits the string where the substring exists. The following example checks a string for an ending pattern, and then partitions the string into three parts by separating at the first occurrence of a whitespace character:
>>>strng.endswith('ere')
True
>>>strng.endswith('here')
True
>>>strng.endswith('tere')
False
>>>g = strng.partition(' ') //Partition returns a value, it does not do anything to "strng"
>>>g
('Hello', ' ', 'There')
-
Related Searches
References
- Photo Credit Jupiterimages/Creatas/Getty Images