Write a program in Python to generate five random even index lowercase alphabets in a series

In this article, we'll explore how to generate five random even-indexed lowercase alphabets and create a Pandas Series from them. Even-indexed characters are those at positions 0, 2, 4, 6, etc. in the alphabet.

Using String Slicing (Recommended)

The most efficient approach uses string slicing with step 2 to extract even-indexed characters −

import pandas as pd
import string
import random

chars = string.ascii_lowercase
print("Lowercase alphabets:", chars)

# Extract even-indexed characters using slicing
even_chars = list(chars[::2])
print("Even-indexed characters:", even_chars)

# Generate 5 random samples
data = random.sample(even_chars, 5)
print("Random even-indexed characters:", data)

# Create Series
result = pd.Series(data)
print("Series:")
print(result)
Lowercase alphabets: abcdefghijklmnopqrstuvwxyz
Even-indexed characters: ['a', 'c', 'e', 'g', 'i', 'k', 'm', 'o', 'q', 's', 'u', 'w', 'y']
Random even-indexed characters: ['s', 'g', 'y', 'a', 'u']
Series:
0    s
1    g
2    y
3    a
4    u
dtype: object

Using Loop with Index Check

Alternatively, you can manually filter even-indexed characters using a loop −

import string
import pandas as pd
import random

# Get all lowercase letters
letters = list(string.ascii_lowercase)

# Find even-indexed characters
even_indexed = []
for i, char in enumerate(letters):
    if i % 2 == 0:
        even_indexed.append(char)

print("Even-indexed characters:", even_indexed)

# Generate random sample and create Series
data = pd.Series(random.sample(even_indexed, 5))
print("Series:")
print(data)
Even-indexed characters: ['a', 'c', 'e', 'g', 'i', 'k', 'm', 'o', 'q', 's', 'u', 'w', 'y']
Series:
0    c
1    i
2    w
3    a
4    g
dtype: object

Comparison

Method Performance Readability Best For
String slicing [::2] Fast High Simple extraction
Loop with enumerate Slower Medium Complex filtering logic

Conclusion

Use string slicing [::2] for extracting even-indexed characters as it's more efficient and readable. The random.sample() function ensures unique selections, and Pandas Series provides a structured output format.

Updated on: 2026-03-25T15:47:49+05:30

180 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements