find charechtar index in string python 1

find charechtar index in string python

myString = 'Position of a character'
myString.find('s')
# 2
myString.find('x')
# -1

Here is what the above code is Doing:
1. We create a string called myString and assign it the value ‘Position of a character’.
2. We call the find() method on myString and pass in the character ‘s’ as an argument.
3. The find() method returns the index of the first occurrence of the character ‘s’ in the string.
4. We call the find() method on myString and pass in the character ‘x’ as an argument.
5. The find() method returns -1 because the character ‘x’ is not present in the string.

Similar Posts