Python program to print occurrence of character in string

0 votes

I am trying to write a program which counts and print the numbers of each character in a string input by the console.

Example: If the following string is given as input to the program:

abcdefgabc

Then, the output of the program should be:

a,2

c,2

b,2

e,1

d,1

g,1

f,1

Please provide the code for this

Jan 9, 2019 in Python by digger
• 26,740 points

edited Jan 9, 2019 by Omkar 30,770 views

3 answers to this question.

+2 votes

Try this:

string=input("Enter the string !!")

newstr=list(string)

newlist=[]

for j in newstr:

    if j not in newlist:

        newlist.append(j)

        count=0

        for i in range(len(newstr)):

            if j==newstr[i]:

                count+=1

        print("{},{}".format(j,count))


Hope it helps!!

If you need to know more about Python, It's recommended to join Python course today.

Thanks!

answered Jan 9, 2019 by Omkar
• 69,220 points
+2 votes

Try the below code

inputString = input("Enter a string: ").casefold()

#casefold() is to ensure that uppercase and lower case charachter is treated same.
#If you don't want, you can remove casefold()

tempStr = ''
for char in inputString:
    if char not in tempStr:
        print(char, ', ', inputString.count(char))
        tempStr = tempStr+char
answered Feb 26, 2019 by Anubhav
• 300 points
thank you too so much .. after searching all the day finally i got it .. thanks again
+4 votes
Try below code

string = input("Enter a string: ")
lst1 = []
for char in string:
    if char not in lst1:
        lst1.append(char)
for item in lst1:
    print(item,string.count(item), sep = ",")
answered May 28, 2020 by Anubhav
• 300 points

Related Questions In Python

+1 vote
2 answers

How to print first character of each word in upper case of a string in Python

class Solution:     def firstAlphabet(self, s):             self.s=s              k=''              k=k+s[0]              for i in range(len(s)):                     if ...READ MORE

answered Oct 28, 2020 in Python by Anurag
12,395 views
+1 vote
1 answer
0 votes
2 answers

Finding the index of a character in python string

You can use word.find('o') as well to ...READ MORE

answered Jun 1, 2018 in Python by george
• 200 points
1,563 views
0 votes
1 answer

How to get the size of a string in Python?

If you are talking about the length ...READ MORE

answered Jun 4, 2018 in Python by aryya
• 7,460 points
1,380 views
–1 vote
2 answers

How to find the size of a string in Python?

following way to find length of string  x ...READ MORE

answered Mar 29, 2019 in Python by rajesh
• 1,270 points
2,117 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,442 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,078 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP