﻿ Python: Generate a random alphabetical character, string and alphabetical strings of a fixed length

# Python: Generate a random alphabetical character, string and alphabetical strings of a fixed length

## Python module: Exercise-3 with Solution

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: ## Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿

## Python: Tips of the Day

Inverts a dictionary with non-unique hashable values:

Example:

```def tips_collect_dictionary(obj):
inv_obj = {}
for key, value in obj.items():
inv_obj.setdefault(value, list()).append(key)
return inv_obj
ages = {
"Owen": 25,
"Jhon": 25,
"Pepe": 15,
}
print(tips_collect_dictionary(ages))
```

Output:

```{25: ['Owen', 'Jhon'], 15: ['Pepe']}
```

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook