Search code examples
pythonstringerror-checking

Is this a correct way in python to find out the that a specific name is in the string for how many max no of times continously


Program for finding a specific name that is in continous no of times. E.g 'yaweryaweryaweraliyawer' here yawer is for 3 maximum no of times continously.

##Created a string to checking for a specific name successive no of times.   

string = 'yaweraliyaweryaweryawer'
##this is the name to find

name = 'yawer'

##max no of times name is in the string 
count = string.count(name)
##reverse loop for checking 
for i in range(count,0,-1):
      if name * count in string:
            print(f"No of successive times {name} is in string is : {count})
            break;

Solution

  • you are almost there, just remove name*count with counter "i"

    like this:

    for i in range(count,0,-1):
          if name * i in string:
                print(f"No of successive times {name} is in string is : {i}")
                break;