1

I'm trying to do a code where is generates a random string, but I have only just started coding so I don't want anything to the code to be too complicated.

import random, string
randomthing = random.choice(string)
print(randomthing(10))

But it keeps saying the length (len) is not defined. What should I do?

styvane
  • 59,869
  • 19
  • 150
  • 156
Izzy
  • 51
  • 1
  • 1
  • 4
  • `string` is a module and module don't have `len` function. Perhaps you want `string.ascii_lowercase`. Also note that `randomthing(10)` will give an `IndexError` – styvane Jun 07 '16 at 09:23
  • 2
    I think what you should do is read a tutorial to learn the differences between a variable, a module and a function. – polku Jun 07 '16 at 09:25
  • Can you explain what this will be used for? What characters do you want to permit in your random string? Just letters? Numbers? Punctuation? Printable ASCII? Arbitrary ASCII? Arbitrary Unicode? – Mark Dickinson Jun 07 '16 at 09:40
  • related: [Fastest method to generate big random string with lower Latin letters](http://stackoverflow.com/q/16308989/4279) – jfs Jun 07 '16 at 12:15

5 Answers5

16

In case, you want to generate unique strings:

import uuid
print uuid.uuid4() # e3c8a1c3-9965-4356-9072-1002632a96e1
print uuid.uuid4().hex # e3c8a1c39965435690721002632a96e1
Vadym
  • 1,444
  • 21
  • 37
Aravindh
  • 535
  • 3
  • 9
9

string module does not literary have len you might want to try this:

Python2:

rand_str = lambda n: ''.join([random.choice(string.lowercase) for i in range(n)])

# Now to generate a random string of length 10
s = rand_str(10)  

Python3:

rand_str = lambda n: ''.join([random.choice(string.ascii_lowercase) for i in range(n)])

# Now to generate a random string of length 10
s = rand_str(10)  

random.choice returns a single character and 10 such characters are joined using the join function.

EDIT

lambda n : ... creates a lambda function which takes n as the argument.

''.join(sequence) joins a sequence into a string with empty string ('' ) between them i.e. it simply joins characters into words.
'.'.join(['a','b','c']) will for example, return a.b.c.

ope
  • 67
  • 1
  • 6
Abdul Fatir
  • 6,159
  • 5
  • 31
  • 58
  • what does the lamba, and join bit do? – Izzy Jun 07 '16 at 09:32
  • @Izzy Edited the answer with an explanation. – Abdul Fatir Jun 07 '16 at 09:38
  • 1
    unrelated: a fast way to generate a random non-deterministic bytestring is `os.urandom(n)`. If you need to limit the allowed bytes; a care should be taken [to avoid the skew, see `write_random_ascii_lowercase_letters()`](http://stackoverflow.com/a/16310739/4279) – jfs Jun 07 '16 at 12:18
2

This has been already answered in Random strings in Python Generating strings from (for example) lowercase characters:

import random, string

def randomword(length):
   return ''.join(random.choice(string.lowercase) for i in range(length))

Results:

>>> randomword(10)
'vxnxikmhdc'
>>> randomword(10)
'ytqhdohksy'
Community
  • 1
  • 1
Vijayanand Premnath
  • 3,415
  • 4
  • 25
  • 42
  • yes, but what does each bit do, for example I am confused on why you add the join and for i bit – Izzy Jun 07 '16 at 09:27
1

Whenever I think about random strings it reminds me about Lorem Ipsum. It exists the loremipsum python package which you can use like this:

from loremipsum import get_sentences
sentences_list = get_sentences(5)

If you don't mind about the exact number of char in the string, it may be a nice solution to generate random strings that look like sentences.

David Guyon
  • 2,759
  • 1
  • 28
  • 40
0

STEP 1:

Generate a random number between 97 and 122 (including both) using random.randint(97,122)

STEP 2:

Convert the random number to a letter using str(unichr(<random number>))

STEP 3:

Append the string to the final string

something like:

randomString=""
for i in range(10):
    randomString += (str(unichr(random.randint(97,122)))) #intended to put tab space.
print randomString                                        #I'm new to Stackoverflow