
10

Python Generate Random String From [a-z0-9]
source link: http://siongui.github.io/2017/02/12/python-generate-random-string/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Python Generate Random String From [a-z0-9]
February 12, 2017
Generate a random string from [a-z0-9] in Python. Read the documentation of random module in Python Standard Library is enough for implementation. random.choice is exactly the function we need to randomly pick up letter from alphabet.
rdnstr.py | repository | view raw
#!/usr/bin/env python # -*- coding:utf-8 -*- import random ALPHABET = "abcdefghijklmnopqrstuvwxyz0123456789" def random_string(strlen): result = "" for i in range(strlen): result += random.choice(ALPHABET) return result if __name__ == '__main__': # for test purpose random.seed() print(random_string(5)) print(random_string(6)) print(random_string(7))
Tested on:
- Ubuntu 16.10
- Python 2.7.12+ & 3.5.2+
References:
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK