Generate two output strings depending upon occurrence of character in input string in Python


In this program , we take a string and count the characters in it with certain condition. The first condition is to capture all those characters which occur only once and the second condition is to capture all the characters which occur more than once. Then we list them out.

Below is the logical steps we are going to follow to get this result.

  • Counter converts the strings into Dictionary which is having keys and value.
  • Then separate list of characters occurring once and occurring more than once using the join()

In the below program we take the input string and

Example

 Live Demo

from collections import Counter
def Inputstrings(load):
   Dict = Counter(load)
   occurrence = [key for (key, value) in Dict.items() if value == 1]
   occurrence_1 = [key for (key, value) in Dict.items() if value > 1]
   occurrence.sort()
   occurrence_1.sort()
   print('characters occurring once:')
   print(''.join(occurrence))
   print('characters occurring more than once:')
   print(''.join(occurrence_1))

if __name__ == "__main__":
   load = "Tutorialspoint has best tutorials"
   Inputstrings(load)

Running the above code gives us the following result −

Output

characters occurring once:
Tbehnp
characters occurring more than once:
ailorstu

Updated on: 19-Dec-2019

51 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements