Python Programes
Python Programes
25)
Code:
#Program to swap values of variable using a third variable
print('Program to swap values of variable')
a=int(input('Variable 1: '))
b=int(input('Variable 2: '))
c=a
a=b
b=c
print("Value of swapped variable(1st): ", a)
print("Value of swapped variable(2nd): ", b)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
#Program to swap values of variable
Variable 1: 5
Variable 2: 2
Value of swapped variable (1st): 2
Value of swapped variable (2nd): 5
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 2(30.04.25)
Code:
#Program to input a number and print its square and cube
print("Program to input a number and print its square and cube")
n = int(input("Enter the number: "))
s = n ** 2
c = n ** 3
print("Square: ", s)
print("Cube: ", c)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output
Enter the number: 5
Square: 25
Cube: 125
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 3(30.04.25)
Code:
#Program to enter side of square and print perimeter and area
print('Program to enter side of square and print its perimeter and area')
s = float(input("Enter the side of square: "))
p=s*4
a = s ** 2
print("Perimeter: ", p)
print("Area: ", a)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
Enter the side of square: 4.1
Perimeter: 16.4
Area: 16.81
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 4(30.04.25)
Code:
#Program to input two numbers and print their sum, difference, product and quotient
print("Program to input two numbers and print their sum, difference, product and quotient")
a = float(input("Enter first number: "))
b = float(input("Enter the second number: "))
s=a+b
p=a*b
d=a-b
q=a/b
print("Sum: ", s)
print("Difference: ", d)
print("Product: ", p)
print("Quotient: ", q)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
Enter first number: 10
Enter the second number: 5
Sum: 15.0
Difference: 5.0
Product: 50.0
Quotient: 2.0
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 5(07.05.25)
Code:
#Program to input name and age and print if eligible to vote
n = input("Enter your name: ")
a = int(input("Enter your age: "))
if a >= 18:
print(n, " you are eligible to vote! Congrats")
else:
print("Sorry", n, "you are not eligible to vote yet.")
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
Enter your name: Sparsh
Enter your age: 16
Sorry Sparsh you are not eligible to vote yet.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 6(07.05.25)
Code:
#Program to input number and print whether the number is even or odd
n = float(input("Enter the number: "))
if n%2 == 0:
print("Number is even.")
else:
print("Number is odd.")
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
Enter the number: 6
Number is even.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 7(07.05.25)
Code:
#Program to input a number and check if it is divisible by 7
n = float(input("Enter the number: "))
if n%7 == 0:
print("The number is divisible by 7.")
else:
print("The number is not divisible by 7.")
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
Enter the number: 5
The number is not divisible by 7.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 8(07.05.25)
Code:
#Program to input a number and print if negative, positive or zero
n = int(input("Enter the number: "))
if n>0:
print("Number is postive.")
elif n<0:
print("Number is negative.")
else:
print("Number is zero.")
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
Enter the number: 5
Number is postive.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 9(07.05.25)
Code:
#User's choice calculator
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
print("Press the respective number to choose operation.")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
op = int(input("Choose operator: "))
if op == 1:
print("Sum: ", a+b)
elif op == 2:
print("Difference: ", a-b)
elif op == 3:
print("Product: ", a*b)
elif op == 4:
print("Quotient: ", a/b)
else:
print("Please choose valid operation")
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
Enter first number: 5
Enter second number: 10
Press the respective number to choose operation.
1. Add
2. Subtract
3. Multiply
4. Divide
Choose operator: 1
Sum: 15.0
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 10(08.05.25)
Code:
#grade student according to their marks
n = input("Enter your name: ")
m1 = float(input("Marks of Subject 1: "))
m2 = float(input("Marks of Subject 2: "))
m3 = float(input("Marks of Subject 3: "))
m4 = float(input("Marks of Subject 4: "))
m5 = float(input("Marks of Subject 5: "))
av = ((m1 + m2 + m3 + m4 + m5)/500)*100
if av >= 85 and av <= 100:
print(n, "you have gotten grade A.")
elif av >= 70 and av < 85:
print(n, "you have gotten grade B.")
elif av >= 60 and av < 70:
print(n, "you have gotten grade C.")
elif av >= 50 and av < 60:
print(n, "you have gotten grade D.")
elif av >= 40 and av < 50:
print(n, "you have gotten grade E.")
elif av < 40 and av >= 0:
print(n, "you have gotten grade F.")
else:
print("Invalid input.")
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
Enter your name: Sparsh
Marks of Subject 1: 98
Marks of Subject 2: 94
Marks of Subject 3: 93
Marks of Subject 4: 92
Marks of Subject 5: 88
Sparsh you have gotten grade A.
Enter your name: Sparsh
Marks of Subject 1: 84
Marks of Subject 2: 78
Marks of Subject 3: 79
Marks of Subject 4: 77
Marks of Subject 5: 85
Sparsh you have gotten grade B.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 11(15.05.25)
Code:
#area calculator
print("Area Calculator")
print("Press the respective number to select shape.")
print("1. Square")
print("2. Rectangle")
print("3. Triangle")
shape = int(input("Choose operation: "))
if shape == 1:
a = float(input("Enter side of square: "))
area = a ** 2
print("Area of square with given side is: ", area)
if shape == 2:
a = float(input("Enter the length of rectangle: "))
b = float(input("Enter the breadth of rectangle: "))
area = a * b
print("Area of rectangle: ", area)
if shape == 3:
a = float(input("First side: "))
b = float(input("Second side: "))
c = float(input("Third side: "))
s = (a + b + c) / 2
A = s-a
B = s-b
C = s-c
area = (s * A * B * C) ** 0.5
print("Area of triangle: ", area)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
Press the respective number to select shape.
1. Square
2. Rectangle
3. Triangle
Choose operation: 1
Enter side of square: 25
Area of square with given side is: 625.0
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 12
Code:
A=int(input("enter first number"))
B=int(input("enter second number"))
C=int(input("enter third number"))
D=A
if D<B:
D=B
if D<C:
D=C
print(D)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
enter first number65
enter second number54
enter third number6
65
-----------------------------------------------------------------
----------------------------------------------------------------
Program 13
Code:
#program to print nmae 10 times input from user
a=input("enter your name:")
for count in range(1,11):
print(a)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
enter your name:sparsh
sparsh
sparsh
sparsh
sparsh
sparsh
sparsh
sparsh
sparsh
sparsh
sparsh
-----------------------------------------------------------------
----------------------------------------------------------------
Program 14
Code:
#print first 20 natural number
for a in range(1,21):
print(a)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
-----------------------------------------------------------------
----------------------------------------------------------------
Program 15
Code:
#program to print first 10 even numbers
for a in range(2,21,2):
print(a)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
2
4
6
8
10
12
14
16
18
20
-----------------------------------------------------------------
----------------------------------------------------------------
Program 16
Code:
#program to print first 10 odd numbers
for a in range(1,20,2):
print(a)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
1
3
5
7
9
11
13
15
17
19
-----------------------------------------------------------------
----------------------------------------------------------------
Program 17
Code:
#input a number from user and print its table
a=int(input("Enter a number to print its table: "))
for b in range(1,11):
print(a,"X",b,"=",a*b)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
Enter a number to print its table: 4
4X1=4
4X2=8
4 X 3 = 12
4 X 4 = 16
4 X 5 = 20
4 X 6 = 24
4 X 7 = 28
4 X 8 = 32
4 X 9 = 36
4 X 10 = 40
-----------------------------------------------------------------
----------------------------------------------------------------
Program 18
Code:
#input a number from user and print its MULTIPLES
a=int(input("Enter a number to print its multiples: "))
for a in range(a,10*a+1,a):
print(a)
OR
#input a number from user and print its multiple
a=int(input("Enter a number to print its multiple: "))
for b in range(1,11):
print(b*a)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
Enter a number to print its multiple: 4
4
8
12
16
20
24
28
32
36
40
Program 20
Code:
#program to print nmae 10 times input from user using while
a=input("enter your name:")
i=1
while i<11:
i+=1
print(a)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
enter your name:sparsh
sparsh
sparsh
sparsh
sparsh
sparsh
sparsh
sparsh
sparsh
sparsh
sparsh
Program 20
Code:
#print first 20 natural even number using while
i=0
while i<20:
i=i+2
print(i)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
2
4
6
8
10
12
14
16
18
20
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 21(project of 11th)
Code:
#menu driven calculator program using while with exit option
while True:
print("enter 1 to add ")
print("enter 2 to subtract ")
print("enter 3 to multiply")
print("enter 4 to divide ")
print("enter 5 to exit ")
c=int(input("enter your your choice:"))
if c==5:
print("bye")
break
elif c<1 or c>5:
print("enter a valid choice")
else:
x=int(input("enter your first number:"))
y=int(input("enter your second number:"))
if c==1:
add=x+y
print("the sum of ",x," and ",y," is ",add)
elif c==2:
sub=x-y
print("the difference of ",x," and ",y," is ",sub)
elif c==3:
prod=x*y
print("the product of ",x," and ",y," is ",prod)
else:
div=x/y
print("the qoutient of ",x," and ",y," is ",div)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
enter 1 to add
enter 2 to subtract
enter 3 to multiply
enter 4 to divide
enter 5 to exit
enter your your choice:1
enter your first number:1
enter your second number:2
the sum of 1 and 2 is 3
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
enter 1 to add
enter 2 to subtract
enter 3 to multiply
enter 4 to divide
enter 5 to exit
enter your your choice:2
enter your first number:1
enter your second number:2
the difference of 1 and 2 is -1
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
enter 1 to add
enter 2 to subtract
enter 3 to multiply
enter 4 to divide
enter 5 to exit
enter your your choice:3
enter your first number:1
enter your second number:2
the product of 1 and 2 is 2
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
enter 1 to add
enter 2 to subtract
enter 3 to multiply
enter 4 to divide
enter 5 to exit
enter your your choice:4
enter your first number:1
enter your second number:2
the qoutient of 1 and 2 is 0.5
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
enter 1 to add
enter 2 to subtract
enter 3 to multiply
enter 4 to divide
enter 5 to exit
enter your your choice:5
bye
enter 1 to add
enter 2 to subtract
enter 3 to multiply
enter 4 to divide
enter 5 to exit
enter your your choice:6
enter a valid choice
enter 1 to add
enter 2 to subtract
enter 3 to multiply
enter 4 to divide
enter 5 to exit
enter your your choice:
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 22
Code:
#check if the input fropm use is prime or composite
n=int(input("enter your no."))
if n==1:
print("composite")
elif n==2 or n==3:
print("prime")
else:
for i in range(2,n):
if n%i==0:
print("composite")
break
else:
print("prime")
OR
#check if the input fropm use is prime or composite or
n=int(input("enter your no."))
flag=0
if n==1:
print("composite")
elif n==2 or n==3:
print("prime")
else:
for i in range(2,n):
if n%i==0:
flag=1
break
if flag==1:
print("composite")
else:
print("prime")
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
enter your no.4
composite
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 23
Code:
#program to input a number and print its factorial
f=1
n=int(input("enter any number"))
for i in range(1,n+1):
f=f*i
print("factorial",n,"is",f)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
enter any number12
factorial 12 is 479001600
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 24
Code:
#program to print fabonacci series
a,b=0,1
n=int(input("enter number of terms"))
print(a)
print(b)
for i in range(n-2):
c=a+b
print(c)
a,b=b,c
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
enter number of terms5
0
1
1
2
3
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 25
Code:
#program to check palindrome number
n=int(input("enter any number="))
a=n
s=0
while(n!=0):
r=n%10
s=s*10+r
n=n//10
if a==s:
print("palindrome number")
else:
print("not palindrome")
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
enter any number=3
palindrome number
enter any number=651
not palindrome
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 26
Code:
#program to print sum of digits of an input no.
a=int(input("enter any number:"))
s=0
while (a!=0):
r=a%10
s=s+r
a=a//10
print(s)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
enter any number:546
15
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 27
Code:
'''program to print pattern(*
**
***
****
*****)'''
for i in range(5):
for j in range(i+1):
print("*",end="")
print()
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
*
**
***
****
*****
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 28
Code:
'''program to print pattern(1
12
123
1234
12345)'''
for i in range(1,6):
for j in range(1,i+1):
print(j,end="")
print()
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
1
12
123
1234
12345
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 29
Code:
'''program to print pattern(1
22
333
4444
55555)'''
for i in range(1,6):
for j in range(i):
print(i,end="")
print()
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
1
22
333
4444
55555
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 30
Code:
'''program to print pattern(55555
4444
333
22
1)'''
for i in range(5,0,-1):
for j in range(i):
print(i,end="")
print()
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
55555
4444
333
22
1
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 31
Code:
'''program to print pattern(12345
1234
123
12
1)'''
for i in range(5,0,-1):
for j in range(1,i+1):
print(j,end="")
print()
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
12345
1234
123
12
1
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 32
Code:
'''program to print pattern(7
89
101112
13141516)'''
a=7
for i in range(1,5):
for j in range(1,i+1):
print(a,end="")
a+=1
print()
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
7
89
101112
13141516
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 33
Code:
'''program to print pattern (11111
2222
333
44
5)'''
a=7
for i in range(1,6):
for j in range(5,i-1,-1):
print(i,end="")
print()
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
11111
2222
333
44
5
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 34
Code:
#program to input a string and check if palindrome
a=input("enter a number:")
str=a
if str==str[::-1]:
print("its a palindrome number")
else:
print("not palindrome")
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
enter a number:66
its a palindrome number
or
enter a number:abc
not plaindrome
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 35
Code:
'''program to print pattern (A
BB
CCC
DDDD
EEEEE)'''
c='A'
a=ord(c)
for i in range(5):
for j in range(i+1):
print(chr(a),end="")
a+=1
print()
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
A
BB
CCC
DDDD
EEEEE
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 36
Code:
'''program to print pattern (ABCDE
ABCD
ABC
AB
A)'''
for i in range(5,0,-1):
c='A'
a=ord(c)
for j in range(i):
print(chr(a),end="")
a+=1
print()
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
ABCDE
ABCD
ABC
AB
A
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 37
Code:
for i in range(5,1,-1):
c='A'
a=ord(c)
for j in range(i):
print(chr(a),end="")
a+=1
print()
c='A'
a=ord(c)
for i in range(5):
for j in range(i+1):
print(chr(a),end="")
a+=1
print()
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
ABCDE
ABCD
ABC
AB
A
BB
CCC
DDDD
EEEEE
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 40
Code:
#program to input a string and count no. of alphabets ,terms , digit and spaces, punctuations
s=input("enter a string :")
a=0
b=0
c=0
d=0
for i in s:
if i.isalpha():
a+=1
elif i.isdigit():
b+=1
elif i.isspace():
c+=1
else:
d+=1
print("no. of alphabets are ",a)
print("no. of digits are ",b)
print("no. of spaces are ",c)
print("no. of punctuations are ",d)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
enter a string :fhgryt47857 479q ;';,.<>.
no. of alphabets are 7
no. of digits are 8
no. of spaces are 3
no. of punctuations are 8
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 41
Code:
#input a string and print no. o f lower and upper case alphabets
s=input("enter a string:")
a=0
b=0
for i in s:
if i.isupper():
a+=1
elif i.islower():
b+=1
print("No. of uppercase aplhabets:",a)
print("No. of lowercase aplhabets:",b)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
enter a string:fgerr4ty78ENC7474YGTG
No. of uppercase aplhabets: 7
No. of lowercase aplhabets: 7
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 42
Code:
#program to input a string and print no. of consonants and vowels
s=input("enter a string:")
c,v=0,0
for i in s.lower():
if i.isalpha():
if i=='a' or i=='e' or i=='i' or i=='o' or i=='u':
v+=1
else:
c+=1
print("no. of vowels:",v)
print("no. of consonents:",c)
or
#program to input a string and print no. of consonants and vowels
s=input("enter a string:")
c,v=0,0
for i in s.lower():
if i.isalpha():
if i in ('AaEeIiOoUu'):
v+=1
else:
c+=1
print("no. of vowels:",v)
print("no. of consonents:",c)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
enter a string:yfgvyryUYHGHDYH747
no. of vowels: 1
no. of consonents: 14
or
enter a string:HEElloooo
no. of vowels: 6
no. of consonents: 3
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 43
Code:
#program to input a string and display the string after replacing space by hyphon
s=input("enter a string:")
st=""
for i in s:
if i==' ':
st=st+'-'
else:
st=st+i
s=st
print(s)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
enter a string:uyhrfyg 75 yt
uyhrfyg-75-yt
-----------------------------------------------------------------
-----------------------------------------------------------------
or
s='computer is fun'
s.replace(' ','-')
'computer-is-fun'
Program 44
Code:
#program to input a string and print all word in separate lines
s=input("enter your input:")
st=s.split()
for i in st:
print(i)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
enter your input:heelooo my world how are you
heelooo
my
world
how
are
you
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 45
Code:
#input a string and print words starting with a
s=input("enter your input:")
st=s.split()
for i in st:
if i[0] in("Aa"):
print(i)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
enter your input:australia is a continent
australia
a
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 46
Code:
#input a string and print words ending with a
s=input("enter your input:")
st=s.split()
for i in st:
if i[len(i)-1] in("Aa"):
print(i)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
enter your input:vada pav are one of the famous foods of india
vada
India
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 47
Code:
#input a string and print words with a
s=input("enter your input:")
st=s.split()
for i in st:
if "a" in(i) or "A" in(i):
print(i)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
enter your input:mumbai is in inida
mumbai
inida
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 48
Code:
#input a string and print no of words
s=input("enter your input:")
st=s.split()
a=0
for i in st:
a+=1
print("no. of terms:",a)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
enter your input:inida is a country
no. of terms: 4
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 49
Code:
#INPUT A STRING AND PRINT THE WORDS HAVING A IN REVeRSE ORDER
s=input("enter your input:")
st=s.split()
for i in st:
if "a" in(i) or "A" in(i):
print(i[::-1])
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
enter your input:mumbai is in india
iabmum
aidni
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 50
Code:
#input a string with multile sentences and print all the sentences in diff lines
a=input("enter any string")
for i in a:
if i in('!?.'):
print()
else:
print(i,end='')
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
enter any stringi live in inida.is india in asia?are you dumb!
i live in inida
is india in asia
are you dumb
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 51
Code:
#input a strin g and the string in such a way that evry alternative character is uppercase
a=input("enter any string")
b=""
for i in range(len(a)):
if i%2==0:
b=b+a[i].upper()
else:
b+=a[i].lower()
print(b)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
enter any stringsparsh
SpArSh
-----------------------------------------------------------------
-----------------------------------------------------------------
Program 52
Code:
#input a string and print longest word
a=input("enter any string ")
b=0
c=a.split()
for i in c:
if b<len(i):
b=len(i)
d=i
print("the longest word:",d)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
enter any string india is in asia
the longest word: india
-----------------------------------------------------------------
-----------------------------------------------------------------