Python Tutorial | Python Strings

6.
Python program to traverse a string and display total vowels in the string

Example Output:
n=”he123o”

output:
total vowels 2

Sol:

#using for loop

n=input("Enter any string ")
v=0
for i in n:
    print(i)
    if(i=='a' or i=='e' or i=='i' or i=="o" or i=='u' or i=="A" or i=='E' or i=='I' or i=='O' or i=='U'):
        v=v+1

print("total vowels ",v)
#using while loop

n=input("Enter any string ")
v=0
i=0
while(i<len(n)):
    print(n[i])
    if(n[i]=='a' or n[i]=='e' or n[i]=='i' or n[i]=="o" or n[i]=='u' or n[i]=="A" or n[i]=='E' or n[i]=='I' or n[i]=='O' or n[i]=='U'):
        v=v+1
    i=i+1

print("total vowels ",v)

7.
Python program to traverse a string and display the following:
Total length
Total alphabets
Total upper case alphabets
Total lower case alphabets
Total digits
Total spaces
Total special characters

Example Output:

Enter any string heLLo12#$ ed$%56

total alphabets 7
total upper case alphabets 2
total lower case alphabets 5
total digits 4
total spaces 1
total special characters 4

Sol:

#using for loop

n=input("Enter any string ")
a=0
lo=0
u=0
d=0
sp=0
spl=0
for i in n:
    if((i>='A' and i<='Z') or (i>='a' and i<='z')):
        a=a+1
        if(i>='A' and i<='Z'):
            u=u+1
        else:
            lo=lo+1
    elif(i>='0' and i<='9'):
        d=d+1
    elif(i==' '):
        sp=sp+1
    else:
        spl=spl+1
        
   
print("total alphabets ",a)
print("total upper case alphabets ",u)
print("total lower case alphabets ",lo)
print("total digits ",d)
print("total spaces ",sp)
print("total special characters ",spl)
#using while loop

n=input("Enter any string ")
i=0
a=0
lo=0
u=0
d=0
sp=0
spl=0
while(i<len(n)):
    if((n[i]>='A' and n[i]<='Z') or (n[i]>='a' and n[i]<='z')):
        a=a+1
        if(n[i]>='A' and n[i]<='Z'):
            u=u+1
        else:
            lo=lo+1
    elif(n[i]>='0' and n[i]<='9'):
        d=d+1
    elif(n[i]==' '):
        sp=sp+1
    else:
        spl=spl+1
    i=i+1
        
   
print("total alphabets ",a)
print("total upper case alphabets ",u)
print("total lower case alphabets ",lo)
print("total digits ",d)
print("total spaces ",sp)
print("total special characters ",spl)

Python Basic Programming Tutorial

Python Introduction     Getting started in Python Programming      Python propgramming fundamentals     Python Operators    Python If Condition     Python for loop    Python range construct      Python While loop    break and continue statements     Different looping techniques     Python List     Python String     Python Functions    Python Inbuilt Functions     Python Recursion     Using Python Library     Python Tuples     Python Dictionary     Python Sets     Python Strings     Python Exception Handling     Python Data File Handling

Visitor Counter

0051075