random string generator

This commit is contained in:
rasbt 2014-06-19 00:32:43 -04:00
parent 2f3ae7cad7
commit c76d80a540
2 changed files with 22 additions and 0 deletions

View File

@ -97,6 +97,9 @@ GitHub repository [One-Python-benchmark-per-day](https://github.com/rasbt/One-Py
- convert 'tab-delimited' to 'comma-separated' CSV files [[IPython nb](http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/useful_scripts/fix_tab_csv.ipynb?create=1)]
- A random string generator [function](./useful_scripts/random_string_generator.py)
<br>

View File

@ -0,0 +1,19 @@
import string
import random
def rand_string(length):
""" Generates a random string of numbers, lower- and uppercase chars. """
return ''.join(random.choice(
string.ascii_lowercase + string.ascii_uppercase + string.digits)
for i in range(length)
)
if __name__ == '__main__':
print("Example1:", rand_string(length=4))
print("Example2:", rand_string(length=8))
print("Example2:", rand_string(length=16))
# Example1: 5bVL
# Example2: oIIg37xl
# Example2: 7IqDbrf506TatFO9