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?

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 Commented Jun 7, 2016 at 9:23
  • 2
    I think what you should do is read a tutorial to learn the differences between a variable, a module and a function. Commented Jun 7, 2016 at 9: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? Commented Jun 7, 2016 at 9:40
  • related: Fastest method to generate big random string with lower Latin letters Commented Jun 7, 2016 at 12:15

5 Answers 5

16

In case, you want to generate unique strings:

import uuid
print uuid.uuid4() # e3c8a1c3-9965-4356-9072-1002632a96e1
print uuid.uuid4().hex # e3c8a1c39965435690721002632a96e1
Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

what does the lamba, and join bit do?
@Izzy Edited the answer with an explanation.
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()
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'

1 Comment

yes, but what does each bit do, for example I am confused on why you add the join and for i bit
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.

Comments

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

1 Comment

Just for Python 3.* users: substitute 'unichr' with 'chr'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.