How To Access Characters In String By Index In Python
Python Strings are the arrays of bytes representing Unicode characters. However, Python does not have the character data type, and a single character is simply a string with a length of 1. In this tutorial, we will see how to access characters in String By Index In Python. Python provides a rich set of operators, functions, and methods for working with strings.
String Indexing in Python
Often in programming languages, individual elements in an ordered set of data can be accessed directly using the numeric index or key value. This process is referred to as indexing.
In Python, the strings are ordered sequences of character data, and thus can be indexed in this way.
Individual characters in the string can be accessed by specifying the string name followed by a number in square brackets ().
Access Characters In String By Index
String indexing is zero-based in Python: the first character in the string has index 0, the next has index 1, and so on.
The index of the last character will be the length of the string – 1.
See the following example.
# app.py str = 'Millie Bobby Brown' print(str)
In Python, the indexing of strings starts from 0 till n-1, where n is the size of the string. So characters in a string of size n can be accessed from 0 to n-1.
In the above code, we are accessing character by index.
Index starts from 0, so we will go for a 7th character, which is B.
➜ pyt python3 app.py B ➜ pyt
We can access the individual characters using an index that starts from 0.
IndexError: string index out of range
If we try to attempt an index beyond the end of the string then it results in an error.
See the following code.
# app.py str = 'Millie' print(str)
➜ pyt python3 app.py Traceback (most recent call last): File "app.py", line 2, in <module> print(str) IndexError: string index out of range ➜ pyt
So, we got an error: IndexError: string index out of range.
TypeError: string indices must be integers
Only Integers are allowed to be passed as the index, float, or other types will cause a TypeError.
See the following code.
# app.py str = 'Emma Watson' print(str[1.5])
➜ pyt python3 app.py Traceback (most recent call last): File "app.py", line 2, in <module> print(str[1.5]) TypeError: string indices must be integers ➜ pyt
We have passed the float value as an index; that is why we got the TypeError: string indices must be integers.
Accessing string characters by negative index
String indices can also be specified with the negative numbers, in which case the indexing occurs from the end of the string backward:
- string[-1] refers to the last character,
- string[-2] the second-to-last character, and so on.
- If string size is n, then string[-n] will return the first character of the string.
See the following code.
# app.py str = 'Emma Watson' print(str[-6]) print(str[-5]) print(str[-len(str)])
➜ pyt python3 app.py W a E ➜ pyt
Attempting to index with the negative numbers beyond the start of the string results in error.
For any non-empty string str, str[len(s)-1] and str[-1] both return the last character. There isn’t any index that makes sense for the empty string.
Modifying characters in a string using 
Let’s try to modify the string by assigning specific characters to a particular position.
# app.py str = 'Emma Watson is Hermione' str = 'K'
➜ pyt python3 app.py Traceback (most recent call last): File "app.py", line 2, in <module> str = 'K' TypeError: 'str' object does not support item assignment ➜ pyt
We will get the TypeError: ‘str’ object does not support item assignment.
String Slicing in Python
Python also allows a form of indexing syntax that extracts substrings from a string, known as string slicing.
If str is a string, an expression of the form str[x:y] returns the portion of s starting with position x, and up to but not including position y.
Let’s say we want to extract Watson from Emma Watson string using slice, and then we can write the following code to achieve that output.
# app.py str = 'Emma Watson is Hermione' print(str[5:11])
➜ pyt python3 app.py Watson ➜ pyt
Again, the second index specifies the first character that is not included in the result. That may seem slightly unintuitive, but it produces this result, which makes sense: the expression str[x:y] will return a substring that is y – x characters in length, in this case, 11 – 5 = 6. So, Watson is 6 characters word.
String indices are zero-based. The first character in a string has an index 0. This applies to both standard indexing and slicing.
Access characters in String are basic operations in any programming language, and Python makes it very easy providing indexing and slicing.
We can access characters by providing a positive and negative index.
Finally, How To Access Characters In String By Index In Python Tutorial Example is over.