Iterator is the object in python which contains some countable values.it can be run with the for and while loop over the string, list, tuple etc.
Iterator contain Two methods
- __iter__ : this method is used for the initialization of the iterator it will return the objects to the __next__.
- __next__ : it is the method which returns the next value in the iterator when it will use as next()
>>> name=["Rohan","Sohan","Sita"] >>> myiterator=iter(name) >>> next(myiterator) 'Rohan' >>> next(myiterator) 'Sohan' >>> next(myiterator) 'Sita' >>>
>>> web="salesforcedrillers" >>> myiter=iter(web) >>> next(myiter) 's' >>> next(myiter) 'a' >>> next(myiter) 'l' >>> next(myiter) 'e' >>> next(myiter) 's' >>> next(myiter) 'f' >>> next(myiter) 'o' >>>
value = "salesforcedrillers" myobj= iter(value) while True: try: printing = next(myobj) print(printing,end=",") except StopIteration: break