[Solved] Distance between two alphabets in a string


You can just use the str.index(char, [beg, end]) method to retrieve the position of a character inside a string. The method allows you to add a beginning and end position that you can use to check for the different occurrences.

Here the code:

s="CNCCN" 
dist = s.index('N', s.index('N') + 1) - s.index('N')

And its output:

print(dist) 
3

Using multiple lines you could avoid calling the s.index(...) method multiple times but you requested one-liner.

2

solved Distance between two alphabets in a string