How do I iterate through the alphabet?

那年仲夏 提交于 2019-12-04 08:47:21

问题


In Python, could I simply ++ a char? What is an efficient way of doing this?

I want to iterate through URLs that have the www.website.com/term/#, www.website.com/term/a, www.website.com/term/b, www.website.com/term/c, www.website.com/term/d ... www.website.com/term/z format.


回答1:


You can use string.ascii_lowercase which is simply a convenience string of lowercase letters,

>>> from string import ascii_lowercase
>>> for c in ascii_lowercase:
...     # append to your url



回答2:


In addition to string.ascii_lowercase you should also take a look at the ord and chr built-ins. ord('a') will give you the ascii value for 'a' and chr(ord('a')) will give you back the string 'a'.

Using these you can increment and decrement through character codes and convert back and forth easily enough. ASCII table is always a good bookmark to have too.



来源:https://stackoverflow.com/questions/17182656/how-do-i-iterate-through-the-alphabet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!