Hướng dẫn python iterate through letters

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

I want to iterate through URLs and generate them in the following way:

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 

JayRizzo

2,7843 gold badges31 silver badges42 bronze badges

asked Jun 19, 2013 at 3:59

1

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

Python 2 Example:

from string import ascii_lowercase

for c in ascii_lowercase:
    # append to your url

Python 3 Example:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from string import ascii_lowercase as alc
for i in alc:
    print(f"www.website.com/term/{i}")

# Result
# www.website.com/term/a
# www.website.com/term/b
# www.website.com/term/c
# ...
# www.website.com/term/x
# www.website.com/term/y
# www.website.com/term/z

Or if you want to keep nesting you can do like so:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
for i in alc:
    print(f"www.website.com/term/{i}")
    for j in alc:
        print(f"www.website.com/term/{i}{j}")

# Result
# www.website.com/term/a
# www.website.com/term/aa
# www.website.com/term/ab
# www.website.com/term/ac
# ...
# www.website.com/term/ax
# www.website.com/term/ay
# www.website.com/term/az
# www.website.com/term/b
# www.website.com/term/ba
# www.website.com/term/bb
# www.website.com/term/bc
# ...
# www.website.com/term/bx
# www.website.com/term/by
# www.website.com/term/bz
# www.website.com/term/c
# www.website.com/term/ca
# www.website.com/term/cb
# www.website.com/term/cc
# ...
# ...
# ...
# www.website.com/term/z
# www.website.com/term/za
# www.website.com/term/zb
# www.website.com/term/zc
# www.website.com/term/zd
# ...
# www.website.com/term/zx
# www.website.com/term/zy
# www.website.com/term/zz

JayRizzo

2,7843 gold badges31 silver badges42 bronze badges

answered Jun 19, 2013 at 4:00

Hướng dẫn python iterate through letters

JaredJared

24.6k7 gold badges52 silver badges61 bronze badges

1

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.

answered Jun 19, 2013 at 4:10

BrianBrian

3,00915 silver badges29 bronze badges

2

shortest way

for c in list(map(chr,range(ord('a'),ord('z')+1))):
    do_something(base_url+c)

iterate function

def plusplus(oldChar):
     return chr(ord(oldChar)+1)
plusplus('a') # output - b

Another option

url=www.website.com/term
my_char=ord('a') # convert char to ascii
while my_char<= ord('z'):
   my_char+=1 # iterate over abc
   my_url=url+chr(my_char)  # convert ascii to char
   do_something(my_url)

Based on @Brian answer.

answered Jun 18, 2021 at 14:47

yoniloboyonilobo

1091 silver badge7 bronze badges