Python: Generate a random alphabetical character, string and alphabetical strings of a fixed length
3. Random Alphabetical Generation
Write a Python program that generates random alphabetical characters, alphabetical strings, and alphabetical strings of a fixed length.
Use random.choice
Sample Solution:
Python Code:
import random
import string
print("Generate a random alphabetical character:")
print(random.choice(string.ascii_letters))
print("\nGenerate a random alphabetical string:")
max_length = 255
str1 = ""
for i in range(random.randint(1, max_length)):
str1 += random.choice(string.ascii_letters)
print(str1)
print("\nGenerate a random alphabetical string of a fixed length:")
str1 = ""
for i in range(10):
str1 += random.choice(string.ascii_letters)
print(str1)
Sample Output:
Generate a random alphabetical character: e Generate a random alphabetical string: wxEGKdCBCkJZrFscEDXhxAovbTkPzlfCxRQCMbuvquFXyFHivyEeqNGzeWxlKZiFzVIuyKLEtPJHbvqQTpIJoMTPFUbrQjGEfXRTlQNKviduRaNDdtEbExXhdbLKiIprgdYTivZDFjk Generate a random alphabetical string of a fixed length: ijdvKiSWwO
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python program to generate a random alphabetical character and a random string of 8 characters using random.choice(), then print them.
- Write a Python script to create a random alphabetical string of fixed length 12 and then reverse the string using slicing.
- Write a Python function that generates a list of 5 random alphabetical strings, each of length 6, using random.choice() and prints the list.
- Write a Python program to create a random string from uppercase letters only, then convert it to lowercase and print both versions.
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to select a random element from a list, set, dictionary (value) and a file from a directory.
Next: Write a Python program to construct a seeded random number generator, also generate a float between 0 and 1, excluding 1.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.