How to Generate Random Words in Python: 3 Easy Methods
There are different ways to generate random words in Python. Here are a few examples:
- Using the random module:
import random
import string
# Generate a random string of length 5
word = ''.join(random.choices(string.ascii_lowercase, k=5))
print(word)
- Using the Faker library:
from faker import Faker
fake = Faker()
# Generate a random word
word = fake.word()
print(word)
- Using the Wordnik API:
import requests
# Get a random word from Wordnik
response = requests.get('https://api.wordnik.com/v4/words.json/randomWord?api_key=<YOUR_API_KEY>')
word = response.json()['word']
print(word)
Note: For the third example, you need to sign up for a Wordnik API key.
原文地址: https://www.cveoy.top/t/topic/lw8D 著作权归作者所有。请勿转载和采集!