So, you are here because you want to learn how to generate random words in Python but do you really know the meaning of random❓
If you don't, lemme tell ya. It's simply the lack of pattern or predictability in events. If something
is said to be random, it has no order and does not follow any pattern or combination.
For example, the numbers, 2,4,6...12 are not random because you can predict the possible outcomes because they are even numbers.
2,655,1,43,4,22 are random because you can not predict the next outcome or possibilities.
Enough talking, let's start coding 😎.
We are going to generate random words in many formats.
As you can see, they are kinda many, don't worry, I will explain the code in detail.
We will start easy and as we progress, things will become hard.😆 Just kidding.
Generating Random Lowercase Words
So in this section, we are going to generate random words which are lowercase letters. Open your favourite IDE and start a new file.
Give the file a nice name and let's start coding.
Well, after running the code, this is what I got ⬇️
[Finished in 0.2s]
Let me now explain my code.
On the first line, I imported the string module. Python's 🐍 string module contains a collection of string constants. Meaning, it contains lowercase letters, uppercase letters, and other constants defined.
Then on the second line, I imported the random module.It contains classes which helps us to generate random objects.
Let's jump to line 4. The variable
lowercase_letters contains all the lowercase letters defined
in it. Meaning, it contains the letters a,b,c,...,y,z.
Let's move on to the function called
lowercase_word. This is the function which generates lowercase random words. The
word variable is the variable
which is going to hold the random word. Initially, it's always empty.
random_word_length is a variable which is going to hold a random number. The
random.randint method takes two parameters, 'a' and 'b'. The 'a' is a number which refers to the minimum random number which can
be generated and 'b' refers to the maximum random number which can be generated. The results is an integer which is stored in the variable
random_word_length. In my case, a and b is 1 and 10 respectively. Meaning the results
is going to be a random number which is in the range of 1 to 10.
To the next line,
while len(word) != random_word_length: is a while loop.
Let me explain, it means while the length of the
is not equal to the random number, it should keep on generating random letters 🆒. Then to the next line,
word += random.choice(lowercase_letters), this statement is repeated in the loop. Each time the loop is repeated,
random.choice selects a random letter from the
lowercase_letters variable and appends it to the
word variable. Notice I used
Then the last line,
return word returns the generated word.
Hey!, that wasn't hard, was it?
random_word = lowercase_word() calls the function
lowercase_word and the function generates a word and assigns it to the
Then on the last line, I just printed it out.
Generating Random Uppercase words
Well, in this section, we will generate random uppercase letters. Open your IDE and let's start coding
Hey, for this one am not going to really explain it. Everything is the same as the
lowercase_word function except that we replaced the code
word += random.choice(lowercase_letters) with
word += random.choice(uppercase_letters).
uppercase_letters contains uppercase letters.
Generating Mixed-Case Random Words In Python
So, over here, we are going to generate random words which are a mix of uppercase and lowercase. Cool right?
Without wasting time, let's start
Also, this one won't get much explanation because it uses the same formats of the other functions.
We only defined a new variable
letters which contains all uppercase and lowercase letters combined.
Then, we replaced the code
word += random.choice(uppercase_letters) with
word += random.choice(letters) and that was all.
Controlling the length of random word
This is very useful if you want the random word to be of a specific length. It's nothing hard.
Let's start coding😎
We are going to use the
lowercase_word function but you can use any of the three functions.
So let me explain, I modified the
lowercase_word function. Let's look at the changes.
Our function now accepts a parameter called
number, Have you seen it is already initialized with a number? The reason is
that, if you choose not to provide a number, it uses the default value in the parameter.
In calling the function, it will be like this
number = lowercase_word(number) with your desired number being in the brackets.
The length of the generated word will be equal to the length of the number you provided.
The rest of the code is pretty self-explanatory.
Determining first and last letters of random word
This part is really simple, we are going to create a function that will make us specify the first and last letter of the random word.
Okay, so we are done, it's not actually that simple though but I will explain.
The function is called
lowercase_word , it's same as the first one, just modified it a bit. So our function takes two parameters,
last_letter. Initially, they are set to None, since it won't be compulsory for you to specify the first and last letters.
So, in the first line of our function, there is a variable
number which contains a random number and it's going to be the length of our random word.
Next, we declared the variable
word which will hold the random word. Then the next statement
if first_letter != None. This means that, if the value of the parameter
first_letter is not None, (Means you have provided a value) it should assign the value of the parameter to the variable
word, since this code runs before the letters are generated, it then becomes the first letter.
Then the next line, will assign the value of parameter to the variable
word only if the value of
first_letter is not None.
To the next line,
while len(word) != number: is the loop in which the random letters are generated.
Then the next line is where it's a little bit tricky.
if len(word) == number - 1:. Okay, let's take it like this. It means, if the length of the
word variable is equal to the random number -1, ...
To simplify it more, it means, before you assign the last letter,...
Then the next line will check if the value of the parameter
last_letter is not None. If it is not None, it will assign the value there to the
word variable instead of generating a new letter.
Then the next line, will return the word.
So let's see ways which we can use the function.