Random Strings in Python

How to create a random string in Python?

I need it to be a number, and then repeating the character until you finish, this is what I created.

def random_id(length): number = '0123456789' alpha = 'abcdefghijklmnopqrstuvwxyz' id = '' for i in range(0,length,2): id += random.choice(number) id += random.choice(alpha) return id 
+81
python
Jan 08 '10 at 7:15
source share
11 answers

Generating strings from (for example) lowercase characters:

 import random, string def randomword(length): letters = string.ascii_lowercase return ''.join(random.choice(letters) for i in range(length)) 

Results:

 >>> randomword(10) 'vxnxikmhdc' >>> randomword(10) 'ytqhdohksy' 
+201
Jan 08 '10 at 19:19
source share

Since this question is fair, uh, random, this might work for you:

 >>> import uuid >>> print uuid.uuid4() 58fe9784-f60a-42bc-aa94-eb8f1a7e5c17 
+48
Jan 08 2018-10-10 at
source share
 >>> import random >>> import string >>> s=string.lowercase+string.digits >>> ''.join(random.sample(s,10)) 'jw72qidagk 
+31
Jan 08 '10 at 19:53
source share

Answer to the original question:

 os.urandom(n) 

Quote: http://docs.python.org/2/library/os.html

Returns a string of n random bytes suitable for use in cryptography.

This function returns random bytes from random random source. The returned data should be unpredictable for cryptographic applications, although its exact quality depends on the OS implementation. On a UNIX-like system, this will request / dev / urandom, and on Windows it will use CryptGenRandom. If the source of randomness is not found, NotImplementedError will be raised.

For a convenient interface with a random number generator on your platform, see random.SystemRandom.

+15
Aug 11 '13 at 13:34 on
source share

You can create random ascii characters, for example:

 import random print chr(random.randint(0,255)) 

And then create a longer line, for example:

 len = 50 print ''.join( [chr(random.randint(0,255)) for i in xrange(0,len)] ) 
+5
Jan 08 '10 at 19:18
source share

You didn’t talk very much about what random string you need. But in any case, you should study the random module.

Below is a very simple solution.

 import random def randstring(length=10): valid_letters='ABCDEFGHIJKLMNOPQRSTUVWXYZ' return ''.join((random.choice(valid_letters) for i in xrange(length))) print randstring() print randstring(20) 
+5
Jan 08 '10 at 7:23
source share
 random_name = lambda length: ''.join(random.sample(string.letters, length)) 
Length

should be <= len (string.letters) = 53. example result

  >>> [random_name(x) for x in range(1,20)] ['V', 'Rq', 'YtL', 'AmUF', 'loFdS', 'eNpRFy', 'iWFGtDz', 'ZTNgCvLA', 'fjUDXJvMP', 'EBrPcYKUvZ', 'GmxPKCnbfih', 'nSiNmCRktdWZ', 'VWKSsGwlBeXUr', 'i stIFGTUlZqnav', 'bqfwgBhyTJMUEzF', 'VLXlPiQnhptZyoHq', 'BXWATvwLCUcVesFfk', 'jLngHmTBtoOSsQlezV', 'JOUhklIwDBMFzrTCPub'] >>> 

Enjoy .;)

+2
May 21 '15 at 17:42
source share

Sometimes I wanted random strings to be illegible, half-forgotten.

 import random def randomWord(length=5): consonants="bcdfghjklmnpqrstvwxyz" vowels="aeiou" return "".join(random.choice((consonants,vowels)[i%2]) for i in range(length)) 

Then,

 >>> randomWord() nibit >>> randomWord() piber >>> randomWord(10) rubirikiro 

To avoid four-letter words, do not set length 4.

Jim

+1
Nov 28 '18 at 23:52
source share

Install this package:

 pip3 install py_essentials 

And use this code:

 from py_essentials import simpleRandom as sr print(sr.randomString(4)) 

More information about the method and other parameters is available here.

0
Feb 11 '18 at 23:40
source share

This function generates a random string consisting of uppercase and lowercase letters, numbers, length delimiter, no_of_blocks to indicate the format of the string

For example: len_sep = 4, no_of_blocks = 4 will generate the following pattern,

F4nQ-Vh5z-JKEC-WhuS

Where, the length delimiter will add a "-" after 4 characters

XXXX-

none of the blocks will generate the next character set as a string

XXXX - XXXX - XXXX - XXXX

if you need one random string, just leave the no_of_blocks variable equal to 1 and len_sep to indicate the length of the random string.

For example: len_sep = 10, no_of_blocks = 1, will generate the following pattern, i.e. random string 10 in length

F01xgCdoDU

 import random as r def generate_random_string(len_sep, no_of_blocks): random_string = '' random_str_seq = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" for i in range(0,len_sep*no_of_blocks): if i % len_sep == 0 and i != 0: random_string += '-' random_string += str(random_str_seq[r.randint(0, len(random_str_seq) - 1)]) return random_string 
0
Feb 24 '18 at 17:28
source share
 import random import string def get_random_string(size): chars = string.ascii_lowercase+string.ascii_uppercase+string.digits ''.join(random.choice(chars) for _ in range(size)) print(get_random_string(20) 

output: FfxjmkyyLG5HvLeRudDS

0
Jan 14 '19 at 2:24
source share



All Articles