Warmachine
Warmachine

Reputation: 15

How to remove spaces from special characters?

So i was working on an python assignment that had this question: Define a function that accepts a sentence and calculate the number of letters(Upper-Lower separately),words,digits,vowels,consonant and special symbols in it. I wrote the following code for this:

def calc():
    ucount=lcount=dcount=vcount=ccount=0
    a = input("Enter the statement :")
    
    for b in a:
        if b.isupper():
            ucount+=1
        elif b.islower():
            lcount+=1
        elif b.isdigit():
            dcount+=1
    for b in a:
        if b>"a" and b<="z" or b>"A" and b<="Z" and a not in "AEIOUaeiou":
            ccount+=1
        elif b in "AEIOUaeiou":
            vcount+=1
        

    b = a.split()
    wcount=len(b)
    c = ucount+lcount+dcount
    scount= len(a)-c
    return ucount,lcount,dcount,vcount,ccount,scount,wcount
u,l,d,v,c,s,w=calc()
print("Number of uppercase characters =", u)
print("Number of lowerrcase characters =", l)
print("Number of digits=", d)
print("Number of vowels =", v)
print("Number of consonant =", c)
print("Number of words =", w)
print("Number of special symbols =", s)

The output is coming but the problem is it is also taking the spaces i give as special characters for example:

Enter the statement :My name is Kunal Kumar
Number of uppercase characters = 3
Number of lowerrcase characters = 19
Number of digits= 0
Number of vowels = 5
Number of consonant = 17
Number of words = 5
Number of special symbols = 4

Please help me to find out how I should remove these spaces from the special characters.

Upvotes: 1

Views: 109

Answers (3)

Mohammed Yousri
Mohammed Yousri

Reputation: 47

You could use replace method

a.replace(" ", "")

Upvotes: 0

Victory
Victory

Reputation: 36

Please read the code below

def calc():
ucount=lcount=dcount=vcount=ccount=0
a = input("Enter the statement :")

for b in a:
    if b.isupper():
        ucount+=1
    elif b.islower():
        lcount+=1
    elif b.isdigit():
        dcount+=1
for b in a:
    if b>"a" and b<="z" or b>"A" and b<="Z":
        if b not in "AEIOUaeiou":
            ccount+=1
    if b in "AEIOUaeiou":
        vcount+=1

b = a.split()
wcount=len(b)
c = ucount+lcount+dcount
scount= len(a)-c
return ucount,lcount,dcount,vcount,ccount,scount,wcount

I have already tested it.

Please make a commit after check.

Upvotes: 0

Sheldon
Sheldon

Reputation: 4643

Just replace the spaces in string a with empty strings prior to computing scount:

scount= len(a.replace(' ',''))-c

Upvotes: 3

Related Questions