Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python – Distance between occurrences

  • Last Updated : 22 Apr, 2020

Sometimes, while working with Python Strings, we can have a task in which we need to find the indices difference between occurrences of a particular character. This can have application in domains such as day-day programming. Lets discuss certain ways in which this task can be done.

Method #1 : Using index()
This is one of the way in which we can solve this problem. In this, we use the power of index() to get the Nth index of occurrence and subtract with initial occurrence.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




# Python3 code to demonstrate working of 
# Distance between occurrences
# Using index()
  
# initializing string
test_str = 'geeksforgeeks'
  
# printing original string
print("The original string is : " + str(test_str))
  
# initializing K 
K = 'k'
  
# Distance between occurrences
# Using index()
res = test_str.index(K, test_str.index(K) + 1) - test_str.index(K)
  
# printing result 
print("The character occurrence difference is : " + str(res)) 
Output :



The original string is : geeksforgeeks
The character occurrence difference is : 8

 

Method #2 : Using find() + rfind()
The combination of above functions can be used to solve this problem. In this, we find the first occurrence using find() and next(last) using rfind().




# Python3 code to demonstrate working of 
# Distance between occurrences
# Using find() + rfind()
  
# initializing string
test_str = 'geeksforgeeks'
  
# printing original string
print("The original string is : " + str(test_str))
  
# initializing K 
K = 'k'
  
# Distance between occurrences
# Using find() + rfind()
res = test_str.rfind(K) - test_str.find(K)
  
# printing result 
print("The character occurrence difference is : " + str(res)) 
Output :
The original string is : geeksforgeeks
The character occurrence difference is : 8



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!