Given a string, which character followed by its frequency, create the appropriate string.
Input : test_str = ‘g7f2g3i2s2b3e4’
Output : gggggggffgggiissbbbeeee
Explanation : g is succeeded by 7 and repeated 7 times.
Input : test_str = ‘g1f1g1’
Output : gfg
Explanation : f is succeeded by 1 and repeated 1 time.
This is one of the ways in which this task can be performed. In this, the task of joining appropriate characters is done using join() and zip() is used to convert different frequency and character strings. The drawback is that frequency of character is restricted to a 1-digit number in this.
The original string is : g7f2g3i2s2b3e4s5t6 The expanded string : gggggggffgggiissbbbeeeessssstttttt
Method #2: Using regex() + join()
This is yet another way in which this task can be performed. In this task of pairing numbers and characters to different strings is performed using regex() and the advantage is that it can take numbers with digits more than 2.
The original string is : g7f2g3i2s2b3e4s5t10 The expanded string : gggggggffgggiissbbbeeeessssstttttttttt
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Python PIL | ImageOps.expand() method
- Python | sympy.expand() method
- Python | Frequency of each character in String
- Python | Construct string from character frequency tuple
- Python | Maximum frequency character in String
- Python - Sort String list by K character frequency
- Python | Find frequency of given character at every position in list of lists
- Python | Insert character after every character pair
- Python program to read character by character from a file
- Find frequency of each word in a string in Python
- Python | Count all prefixes in given string with greatest frequency
- Python | Frequency of substring in given string
- Python - Prefix frequency in string List
- Python | Frequency of numbers in String
- Python - Bigrams Frequency in String
- Python - Words Frequency in String Shorthands
- Python - All substrings Frequency in String
- Python - Frequency of K in sliced String
- Python - List Words Frequency in String
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.