Generating two output lines depending on the occurrence of a character in an input string in Python

Counters | Python Methods and Functions | String Variables

Examples:

 Input: str = "pythonengineering" Output: String with characters occurring once: "for". String with characters occurring multiple times: "egks" Input: str = "geekspractice" Output: String with characters occurring once: "agikprst" String with characters occurring multiple times: "ce" 

we have a solution to this problem, please refer to the to the input string . We can quickly fix this problem in python using the

# Function Generate two output lines depending on
# character occurrence in input line

  

from collections import Counter

 

def generateStrings ( input ):

  

# convert string to dictionary

# having characters as keys and frequency as value

freqDict = Counter ( input )

 

# highlight characters with a frequency of 1 or more of 1

  freq1 = [key for (key, count) in freqDict .items () if count = = 1 ]

  freqMore1 = [key for (key, count) in freqDict.items () if count & gt ; 1 ]

  

# sorting lists and concatenating characters

# no space to print the resulting lines

freq1.sort ()

freqMore1.sor t ()

 

# print output lines

print ( 'String with characters occurring once:' )

print ('' .join (freq1))

print ( 'String with characters occurring multiple times:' )

print ('' .join (freqMore1))

 
# Driver program

if __ name__ = = "__ main __" :

input = "pythonengineering"

generateStrings ( input )

Output:

 String with characters occurring once: for String with characters occurring multiple times: egks 




Get Solution for free from DataCamp guru