String length without len function

前端 未结 17 1310
逝去的感伤
逝去的感伤 2020-12-10 21:29

Can anyone tell me how can I get the length of a string without using the len() function or any string methods. Please anyone tell me as I\'m tapping my head ma

17条回答
  •  半阙折子戏
    2020-12-10 21:58

    Here's a method which isn't using neither len nor iteration:

    >>> a = 'a' * 200000
    >>> a.rindex(a[-1]) + 1
    200000
    

    To make it work for lists, which don't have rindex, use:

    >>> a = list(range(200000))
    >>> a.index(a[-1], -1) + 1
    200000
    

提交回复
热议问题