Python Program to Expand Character Frequency String

A character frequency string contains characters followed by their frequency count. For example, 'p5y3t6' means 'p' appears 5 times, 'y' appears 3 times, and 't' appears 6 times. Expanding this string produces 'pppppyyytttttt'.

In this article, we will learn how to expand character frequency strings using different Python approaches.

Problem Statement

Given an input string where characters alternate with their frequencies, expand each character according to its frequency count ?

Example

inputString = 'p5y3t6h2o1n4'
print("Input:", inputString)
print("Expected Output: pppppyyytttttthhonnnn")
Input: p5y3t6h2o1n4
Expected Output: pppppyyytttttthhonnnn

Method 1: Using zip() and join()

The zip() function combines iterators, while join() concatenates strings. We can pair characters with their frequencies using slicing ?

inputString = 'p5y3t6h2o1n4'
print("Input String:", inputString)

# Pair characters (even indices) with frequencies (odd indices)
expandStr = "".join(char * int(freq) 
                   for char, freq in zip(inputString[0::2], inputString[1::2]))

print("Resultant string after expanding:", expandStr)
Input String: p5y3t6h2o1n4
Resultant string after expanding: pppppyyytttttthhonnnn

How It Works

  • inputString[0::2] extracts characters at even positions: 'p', 'y', 't', 'h', 'o', 'n'

  • inputString[1::2] extracts frequencies at odd positions: '5', '3', '6', '2', '1', '4'

  • zip() pairs each character with its frequency

  • char * int(freq) repeats the character frequency times

Method 2: Using Regular Expressions

The re.findall() function can match character-frequency patterns using regex ?

import re

inputString = 'p5y3t6h2o1n4'
print("Input String:", inputString)

# Match character followed by optional digits
expandStr = ''.join(char * int(freq or 1)
                   for char, freq in re.findall(r'(\w)(\d+)?', inputString))

print("Resultant string after expanding:", expandStr)
Input String: p5y3t6h2o1n4
Resultant string after expanding: pppppyyytttttthhonnnn

Regex Pattern Explanation

  • (\w) captures any word character (letters, digits)

  • (\d+)? optionally captures one or more digits

  • freq or 1 defaults to 1 if no frequency is found

Method 3: Manual Parsing

This approach manually separates characters and frequencies without built-in functions ?

inputString = 'p5y3t6h2o1n4'
print("Input String:", inputString)

# Separate characters and frequencies
chars = []
frequencies = []

for i in range(len(inputString)):
    if i % 2 == 0:  # Even indices are characters
        chars.append(inputString[i])
    else:  # Odd indices are frequencies
        frequencies.append(int(inputString[i]))

# Build expanded string
expandStr = ""
for i in range(len(chars)):
    expandStr += chars[i] * frequencies[i]

print("Resultant string after expanding:", expandStr)
Input String: p5y3t6h2o1n4
Resultant string after expanding: pppppyyytttttthhonnnn

Comparison

Method Time Complexity Space Complexity Best For
zip() + join() O(n) O(n) Clean, Pythonic code
Regular Expressions O(n) O(n) Complex patterns
Manual Parsing O(n) O(n) Educational purposes

Conclusion

The zip() method provides the most readable solution for expanding character frequency strings. Regular expressions offer flexibility for complex patterns, while manual parsing helps understand the underlying logic.

---
Updated on: 2026-03-27T12:59:29+05:30

505 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements