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 31,129 views
How would you get this code to print as in 'There are x uppercase, Y lowercase and Z numbers in this string'?

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
How would you get this code to print as in 'There are x uppercase, Y lowercase and Z numbers in this string'?
+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
0 votes

Consider a list of characters (characters may be alphabets, special characters, digits). Write a Python program to do the following:  

1) Count total number of elements in the list  

2) Count total number of vowels in the list (vowels are ‘a’, ‘e’, ‘i’, ‘o’, ‘u’) 

3) Count total number of consonants in the list (a consonant is an alphabet other than vowel)  

4) Count total number of characters other than vowels and consonants  

Display all the values with appropriate titles.  

listChar = ['$','a','8','!','9','i','a','y','u','g','q','l','f','b','t']

c = 0

cVowel = 0

cConst = 0

cOther = 0

for i in listChar :

    c += 1

    if i in 'aeiou' :

        cVowel = cVowel + 1

    elif i in '!@#$%^&*()+-*/123456789~`' :

        cOther = cOther + 1

    else :

        cConst = cConst + 1

print ("total number of element in the list  : ", c)

print("count vowels characters : ",cVowel)

print("count consonants characters : ",cConst)

print("count other characters : ",cOther)

answered Apr 14, 2021 by vishakha

edited 5 days ago
0 votes
Write a Python program to take input of a string from the user and then create a dictionary  containing each character of the string along with their frequencies. (e.g. if the string is ‘banana’  then output should be {'b': 1, 'a': 3, 'n': 2}.

d = dict()

user = input ("enter a string ::-- ")

lst = list(user)

l = len(lst)

for i in range(l) :

    c = 0

    for j in range(l) :

        if lst[i] == lst[j ]:

            c += 1

    d[lst[i]] = c

print("dictionary is  :: ",d)
answered Apr 14, 2021 by vishakha

edited 5 days ago
0 votes
str3 = 'abcdefgabc'
final_lst = []
for char in str3:
    if char not in final_lst:
        final_lst.extend([char,str3.count(char)])

it = iter(final_lst)
for x in it:
    print(x, next(it), sep = ",")
answered Jul 4, 2021 by Aalhad

edited 5 days ago
0 votes
n=input()
l={}
for x in n:
    if x  in l:
        l[x]=l[x]+1
    else:
        l[x]=1
for z,m in l.items():
    print("{}{}".format(z,m),end="")
answered Aug 30, 2021 by Satya mca

edited 5 days ago
0 votes

Try it:

str=input('Enter a string:')
str1=set(str)
str1=list(sorted(str1))
for i in range(len(str1)):
    x=str.count(str1[i])
    print(str1[i],x)
answered Jun 9, 2022 by Rukmini Munjam

edited 5 days ago

Related Questions In Python

+1 vote
2 answers

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

text = "this is a sample python ...READ MORE

answered Jun 22, 2022 in Python by anonymous

edited 5 days ago 12,642 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,704 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,536 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,409 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,639 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,410 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