Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python finding the position of a specific character in a string

Python finding the position of a specific character in a string

Document Object Reference Manual Python3 Examples

In Python, we can use find() method to find the position of a specific character in a string.find() method returns the index of the first occurrence of the character in the string, or -1 if the character does not exist.

Example

Define a string

text

=

"Hello, welcome to the world of Python."

Find the position of the character 'w'

position

=

text.

find

(

'w'

)

Output result

print

(

"The position of 'w' is:"

,

position

)

Code analysis:

Output result:

The position of 'w' is: 7

In this example, the character 'w' has an index position of 7 in the string.

Document Object Reference Manual Python3 Examples