How to Find the Position of a Character or String within a String in Python 


               


How to Find the Position of a Character or String within a String in Python



Python


In this article, we show how to find the position of a character or string within a string in Python.

This can be done with the index() function in Python.

The index() function returns the position of the character or string.

For example, if we have the string, "beach" and and find the position of 'e' using the index() function, 1 will be returned. This is because the count always start with an index of 0.

This is shown in the code below.



The same thing can be done with strings.

So if you wanted to find the position of a string within a string, it would be done in the same manner with the index() function.

Note that when you find the position of a string within a string, the value returned is the position of the first character of the string.

This is shown in the code below.



So the value returned is 2 because the position of 'w' in 'went' is in the 2nd position.

And this is all that is needed to find the position of a character or string within a string in Python.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...