Create a random string in Python

random.py


#Hexadecimal(0-9, a-f)
def gen_rand_str_hex(length):
    import os
    import hashlib
	buf = ''
	while len(buf) < length:
		buf += hashlib.md5(os.urandom(100)).hexdigest()
	return buf[0:length]


#Uppercase and lowercase letters+Numbers(0-9, a-z, A-F)
def gen_rand_str(length, chars=None):
    import random
    import string
	if chars is None:
		chars = string.digits + string.letters
	return ''.join([random.choice(chars) for i in range(length)])


Recommended Posts

Create a random string in Python
Create a function in Python
Create a dictionary in Python
Create a datetime object from a string in Python (Python 3.3)
Create a DI Container in Python
Create a binary file in Python
Create a Kubernetes Operator in Python
Create a simple GUI app in Python
Create a JSON object mapper in Python
[GPS] Create a kml file in Python
Generate a class from a string in Python
Create a Python module
String manipulation in python
Create SpatiaLite in Python
Random walk in Python
Random string generation (Python)
Create a Python environment
Create an instance of a predefined class from a string in Python
Create a Vim + Python test environment in 1 minute
Create a GIF file using Pillow in Python
How to embed a variable in a python string
I want to create a window in Python
Create a standard normal distribution graph in Python
How to create a JSON file in Python
Create a virtual environment with conda in Python
Create a simple momentum investment model in Python
Create a new page in confluence with Python
Create a package containing global commands in Python
Create a MIDI file in Python using pretty_midi
Create a loop antenna pattern in Python in KiCad
Put out a shortened URL string in Python
Insert an object inside a string in Python
[Docker] Create a jupyterLab (python) environment in 3 minutes!
Balanced Random Forest in python
Create a Wox plugin (Python)
String object methods in Python
[Python] Use a string sequence
Create gif video in Python
Use Random Forest in Python
Make a bookmarklet in Python
Create a python numpy array
String date manipulation in Python
Draw a heart in Python
Create a directory with python
Parse a JSON string written to a file in Python
Create a data collection bot in Python using Selenium
Create your own Random Dot Stereogram (RDS) in Python.
[LINE Messaging API] Create a rich menu in Python
Create a plugin to run Python Doctest in Vim (2)
In Python, create a decorator that dynamically accepts arguments Create a decorator
Create a fake Minecraft server in Python with Quarry
Maybe in a python (original title: Maybe in Python)
[Python] Random processing (create, select, sort)
Testing with random numbers in Python
Find out the apparent width of a string in python
[python] Manage functions in a list
Create a local scope in Python without polluting the namespace
Create a list in Python with all followers on twitter
Create a python GUI using tkinter
Create a pandas Dataframe from a string.
Create a Python environment on Mac (2017/4)