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

PythonServer Side ProgrammingProgramming

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


 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]
   print('characters occurring once:')
   print('characters occurring more than once:')

if __name__ == "__main__":
   load = "Tutorialspoint has best tutorials"

Running the above code gives us the following result −


characters occurring once:
characters occurring more than once:
Updated on 19-Dec-2019 12:32:58