I'm trying to understand why I can iterate along the string. What I see in the documentation is:
One method needs to be defined for container objects to provide iteration support:
container.__iter__()
Return an iterator object. The object is required to support the iterator protocol described below. If a container supports different types of iteration, additional methods can be provided to specifically request iterators for those iteration types. (An example of an object supporting multiple forms of iteration would be a tree structure which supports both breadth-first and depth-first traversal.) This method corresponds to the tp_iter slot of the type structure for Python objects in the Python/C API.
The iterator objects themselves are required to support the following two methods, which together form the iterator protocol:
iterator.__iter__()
Return the iterator object itself. This is required to allow both containers and iterators to be used with the for and in statements. This method corresponds to the tp_iter slot of the type structure for Python objects in the Python/C API.
iterator.next()
Return the next item from the container. If there are no further items, raise the StopIteration exception. This method corresponds to the tp_iternext slot of the type structure for Python objects in the Python/C API.
But...
>>> dir('aa')
['__add__', '__class__', '__contains__', '__delattr__', '__doc__',
'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__',
'__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__',
'__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__',
'__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
'_formatter_field_name_split', '_formatter_parser', 'capitalize',
'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs',
'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower',
'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip',
'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition',
'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip',
'swapcase', 'title', 'translate', 'upper', 'zfill']
I don't see here any __iter__() or next(). So why does it work?
In this tutorial, you will find out different ways to iterate strings in Python. You could use a for loop, range in Python, slicing operator, and a few more methods to traverse the characters in a string. The following are various ways to iterate the chars in a Python string.
A thing as simple as iterating over each character in a string is one of them. Let’s explore some methods and discuss their upsides and downsides. Before we start, we need to come back to a much more basic question. Nowadays with ECMAScript 2015 (ES6), there are two ways of accessing a single character:
It cuts off a substring from the original string and thus allows to iterate over it partially. The [] operator has the following syntax: To use this method, provide the starting and ending indices along with a step value and then traverse the string.
One downside is you need to convert the string into an array before iterating. If performance really matters in your use case (and it usually doesn’t), it might not be your first choice. As with many techniques in JavaScript, it’s mainly a matter of taste when deciding which one to use.
Iterators were new in Python 2.2. The old method was the sequence protocol (implements __getitem__
with 0-based indices) and still works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With