How to generate random sentences in Python

In this tutorial, we will discuss how to generate random sentences in Python.

There are many ways to generate random sentences using random and secret modules. In this tutorial, we are going to cover major possible ways to accomplish this task with examples.

The random module is used for generating random elements and also for shuffling the elements. Some of the methods from this module can be used to generate random sentences.

Below is the ways that we are going to discuss to perform our given task:

  • essential-generators
  • randint method
  • random.choice method
  • secret.choice method

Now we are going to how to perform our task using each of these methods:

Generate random sentences in Python using essential-generators

This is an inbuilt module in Python which is useful for generating random words, sentences, paragraphs. This can be installed by the following command:

pip install essential-generators

Let’s see the code:

# import documentgenerator
from essential_generators import DocumentGenerator
main = DocumentGenerator()
print(main.sentence())

Output:

According to the south and west of cumuliform Catholicism a differential status.

Generate random sentence in Python using random.randint

random.randint() used to generate random numbers and sentences.

Syntax:

random.randint(start,end)

Note that start and end should be integer values.

If float values are given in parameters then it will throw a ValueError

If other than numeric values are assigned to start and end then it will throw a TypeError

Initially, we declare some set of names, verbs, and nouns to form a random sentence.

Let’s see the code:

# import random module
from random import randint

# Declaring names, verbs and nouns
names=["You","I","They","He","She","Robert","Steve"]
verbs=["was", "is", "are", "were"]
nouns=["playing cricket.", "watching television.", "singing.", "fighting.", "cycling."]

print(names[randint(0,len(names)-1)]+" "+verbs[randint(0,len(verbs)-1)]+" "+nouns[randint(0,len(nouns)-1)])

Output:

She is singing.

Generate random sentence in Python using random.choice

random.choice() is another method to select random elements.

Syntax:

random.choice(sequence)

sequence can be list, set or tuple.

Initially, we declare some set of names, verbs, and nouns to form a random sentence. we will use random.choice() to get the random elements from the list.

Let’s see the code:

# import random module
import random

# Declaring names, verbs and nouns
names=["You","I","They","He","She","Robert","Steve"]
verbs=["was", "is", "are", "were"]
nouns=["playing cricket.", "watching television.", "singing.", "fighting.", "cycling."]

a=(random.choice(names))
b=(random.choice(verbs))
c=(random.choice(nouns))
print(a+" ",b+" ",c)

Output:

They are fighting.

Generate random sentence in Python using secret module

Secret module is an inbuilt function used to generate random elements.

Syntax:

secret.choice(sequence)

sequence can be list, set or tuple.

Initially, we declare some set of names, verbs, and nouns to get a random sentence. We will use secret.choice() to get the random elements from the list.

Let’s see the code:

# import secrets module
import secrets

# Declaring names, verbs and nouns
names=["You","I","They","He","She","Robert","Steve"]
verbs=["was", "is", "are", "were"]
nouns=["playing cricket.", "watching television.", "singing.", "fighting.", "cycling."]

a=(secrets.choice(names))
b=(secrets.choice(verbs))
c=(secrets.choice(nouns))
print(a+" ",b+" ",c)

Output:

They were watching television.

Also read:

Leave a Reply

Your email address will not be published.