Mohit Raj
Posted on December 23, 2019
Indexing in Python string
Strings are ordered sequences of character data. Indexing allows you to access individual characters in a string directly by using a numeric value. String indexing is zero-based: the first character in the string has index 0, the next is 1, and so on. In this post, I’ll show you string indexing syntax and practice with several examples
From the above figure-
- first character in the string has index 0, the next is 1, and so on.
- If start indexing from the last character then you have to start with -1 and as you come near to first character negative value increases by 1 each time. this is also called negative indexing.
How to access a character in string:
Simple answer is by using the index of all the characters in the string.
Example:
s='Hello'
#if i want to access 'H' in Hello then
print("First character is : "s[0]) # indexing stated from 0
# if i want to access 'o' in s
print(" Last character using positive indexing : ",s[4])
#another way
print("last charcater using negative indexing : ",s[-1]) # using negative indexing
Output is:
First character is : H
Last character using positive indexing : o
last charcater using negative indexing : o
If we pass a index value which not exist for that string then it will return an error
s='python'
print("Third character is : ",s[2])
print("Seven character is : ",s[7]) # which is not possible for given string
Output gives an error for the second value
Third character is : t
Seven character is :
IndexError: string index out of range
Python String index() Method
- The index() method finds the first occurrence of the specified value.
- The index() method raises an exception if the value is not found.
Syntax
string.index(value, start, end)
value(Required.) -The string to be searched for.
start(Optional.)- This function specifies the position
from where search has to be started. Default is 0
end(Optional.) - This function specifies the
position from where search has to end. Default is to the end of the string
Return the starting index number of the passed valued according to the start and end value passed.
By default start value is 0 and end value is string length.
Example
sentence='hello world and hello python'
value='h'
x=sentence.index(value) # start and end is default value
print(x)
y=sentence.index(value,2) # end value is default
print(y)
z=sentence.index(value,6,8) # give error because in between 6 and 8 'h' not exist.
print(z)
value='and'
a=sentence.index(value,6,20)
print(a)
Output is :
0
16
ValueError: substring not found
12
Python String find() Method
- The find() method finds the first occurrence of the specified value.
- The find() method returns -1 if the value is not found.
- The find() method is almost the same as the index() method, the only difference is that the index() method raises an exception if the value is not found.
- If value is not found find() method return -1.
Syntax
string.find(value, start, end)
value(Required.) -The string to be searched for.
start(Optional.)- This function specifies the position
from where search has to be started. Default is 0
end(Optional.) - This function specifies the
position from where search has to end. Default is to the end of the string
Example:
sentence='hello world and hello python'
value='h'
x=sentence.index(value) # start and end is default value
print(x)
y=sentence.index(value,2) # end value is default
print(y)
z=sentence.index(value,6,8) # Retuen -1 because value not found between 6 and 8.
print(z)
value='and'
a=sentence.index(value,6,20)
print(a)
Output is :
0
16
-1
12
Posted on December 23, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.