python programs on string

 Python Program on String

Program on String we will learn first .

String is sequential and immutuable datatype .Immutuable means not changable ,it means we cannot change the element in the given string.

So Let us understand how to find length in the given string .


Testcases1:

Input:

Python

Output:

6

Testcases2:

This is python

Output:

14


#Let us understand about string that how to find lenght of string and how indexing in string

s=Python language

#Here lenght of string is 15 it counts number of alphabets and also number of spaces

s="Python language"
#now we will use len() to find length of given string 
print(len(s))


Now you have understood that how to calculate length of any string.

So now we will calculate length of the without function.

#Now we will find length off given without using  len() function
s="python language"

#First take length of given string is 0
l=0
#Take a for loop to access every element of the string if character in the string then l=l+1
for ch in s:
    l=l+1
print(l)


Indexing In String:

#Now indexing in string

0 1  2  3  4 5  6  7 8  9  10 11 12  13   14
P y  t  h  o n    l  a  n  g  u   a   g   e

#this is called forward indexing that start from 0 to length of given string -1

Now we will understand about backward indexing in string
-15  -14  -13 -12  -11  -10 -9  -8  -7  -6  -5  -4  -3  -2  -1
P    y    t   h    o    n       l   a   n   g   u   a   g   e


This is the basic of the string that about indexing in he string of every element.

Representation of index value i.e square box

s[0] means first element of the string.

Examplre:

S="Language"

S[0]=L

S[1]=a

S[2]=n

S[3]=g


Testcases1:

Input:

s=Python 

print(s[0])

Output:

P


Testcases2:

Input:

s=Python

s[1] 

Output:

y

Program on string to calculate number of spaces ibn the string

Testcases1:

Input:

This is python

Output:

2

Testcases2:

Python language

Output:

1


#Now we will move forward to count number of spaces in the given string for that you can input the string using input() function Let see
s=input("Enter a string:")
# To count any thing you must have to always take any variable its value is 0 in the case when something have to count

count=0
#Now to access every element of the string take for loop and test a condition if there is any space that is denoted by ch then count+=count+1
for ch  in s:
    if ch==" ":
        count=count+1
#Now use print() to print total number of spaces in the string

print(count)


Program to check given string is pallindrome or not

pallindrome means its inverse must be same as it given

Testcases1:

Input:

radar

Output:

pallindrome

Testcases2:

Radar

Ouput:

Not pallindrome

#First input string using input() function


str1=input("Enter a string:")
#Take a another variable and store value of the input string in descending order
str2=str1[-1::-1]
#Now test a condition that if input string is similar to its inverse 

if str1==str2:
        print("Given string is pallindrome.")
else:
        print("Give string is not pallindrome.")













Program On string to calculate number of vowels in the string.

Now We will make a program to count vowels in the given string 

Testcases1:

Input

python

Output:

1

Testcases2:

Input:

Mango

Output:

2

#So similarily we will count number of vowels in the string 
# First Input string using input() function
S=input("Enter a string:")
#similarily take Count=0
Countvowels=0
#And then for loop to access the string elements  then we will check every element of the string that they are vowels or not if ch value is equal to a,e,i,o,u ,A,I,E ,O,U then count will increase by +1
for ch in S:
    if ch=='a' or ch=='u'or ch=='e' or ch=='i' or ch=='o' or ch=='A' or ch=="U" or ch=='I' or ch=='O' or ch=='E':
        
        Countvowels=Countvowels+1

    else:
        pass

#Now use print() function to print number of vowels in the string
print(Countvowels)



Similarily we will calculate number of alphabets ,digits,symbol and spaces in the given string.

Testcases1:

Input:

This is python 3 @

Output:

12

1

4

1

Testcases2:

Python 2 to 3@

Output:

8

3

3

1

# Now we will forward to count number of vowels ,uppercaseletter ,lowercase letter  and Number in the input string
#First use input() to take take input of string
str=input("Enter a string:")

print(str)
#Then we will assume number of spaces,symbols,digits and alphabets is equal to  0 using assignment operator

countalpha,countdigit,countspaces,other=0,0,0,0,


#Now we will take for loop t access every element of the string to check is it digit or symbol,or alphabet or spaces if any condition is true then count that thing By +1
for ch in str:

#This is called if elif else structure i.e test part

    if (ch>='A' and ch<='Z') or ( ch>='a' and ch<='z'):

        

        countalpha=countalpha+1                          # or we write countalph+=1

    elif ch>='0' and ch<='9':

        countdigit=countdigit+1

    elif ch==' ':

        countspaces=countspaces+1

    else:

        other=other+1 #Here other means symbol
        
#After testing the all conditions now use print() to print number of spaces ,number of alphabets ,number of digits  and symbols present in the string

print("In a given string there are ",countalpha,"alphabets.")

print("In a given string there are ",countdigit," digits.")

print("In a given string therea are " ,countspaces,"spaces.")

print("In a given string there are ",other,"symbol.")
        










































































No comments:

Powered by Blogger.