DELHI PUBLIC SCHOOL-BOPAL, AHMEDABAD
Class: IX Subject: Artificial Intelligence Session: 2025-2026
PRACTICAL FILE
Programming with Python String
#1. WAP to display each character of the string one by one.
s1=input(“Enter a string-”)
for i in s1:
print(i)
3
#alternate method
L=len(s1)
for i in range(L):
print(s1[i])
#2. WAP to display each character of a string in a line
s1=input(“Enter a string-”)
for i in s1:
print(i, end= " ")
#3. WAP to find the length of a string
s1=input(“Enter a string-”)
count=0
for i in s1:
count=count+1
print("Length of the string=",count)
#4. WAP to reverse a string
s1=input(“Enter a string-”)
rev= ""
for i in s1:
rev=i+rev
print("Reverse of the string=",rev)
# alternate method
rev=s1[::-1]
print("Reverse of the string using slicing=",rev)
#5. WAP to count alphabets, digits & special character in a string
s1=input(“Enter a string-”)
a,d,s=0,0,0
for i in s1:
if (i>= "A" and i<= "Z") or (i>= "a" and i<= "z"):
a+=1
elif i>= "0" and i<= "9":
d+=1
else:
s+=1
print("No. of alphabets=",a)
print("No. of digits=",d)
print("No. of special characters=",s)
#6. WAP to count lowercase, uppercase & spaces in a string
s1=input("Enter a string-”)
l,u,s=0,0,0
for i in s1:
if i>= "A" and i<= "Z":
u+=1
elif i>= "a" and i<= "z":
l+=1
elif i==" ":
s+=1
print("No. of uppercase=",u)
print("No. of lowercase=",l)
print("No. of spaces=",s)
#7. WAP to count vowels & consonants in a string
s1=input("Enter a string-")
v,c=0,0
for i in s1:
if i in "AEIOUaeiou":
v+=1
elif (i>= "A" and i<= "Z") or (i>= "a" and i<= "z"):
c+=1
print("No. of Vowels=",v)
print("No. of consonants=",c)
#8. WAP to convert all uppercase to lowercase to uppercase and
#keep rest of the characters as it is.
s1=input("Enter a string-")
new= ""
for i in s1:
if i>= "A" and i<= "Z":
new=new+chr(ord(i)+32)
elif i>= "a" and i<= "z":
new=new+chr(ord(i)-32)
else:
new=new+i
print("The original string is", s1)
print("New String is ",new)
#9. WAP to count the number of characters in a string
#without using string function
s1=input("Enter a string-”)
leng=0
for i in s1:
leng=leng+1
print("Length of the string=",leng)
#10. WAP to convert the string to lowercase letter
#without using string function
s1=input("Enter a string-”)
s2= ""
for i in s1:
if i>= "A" and i<= "Z":
s2=s2+chr(ord(i)+32)
else:
s2=s2+i
print("string in lowercase-", s2)
#11. WAP to replace all digits with “@” and all special characters with “#”
# keep all the other characters as it is
s1=input("Enter a string-”)
s2= ""
for i in s1:
if i>= "0" and i<= "9":
s2=s2+"@"
elif (i>= "A" and i<= "Z") or (i>= "a" and i<= "z"):
print("")
else:
s2=s2+ "#"
print("New String=", s2)
#12. WAP to check whether the given string is a palindrome or not
s1=input("Enter a string-")
leng=len(s1)-1
for i in s1:
if i!=s1[leng]:
print(s1, "is not a palindrome")
break
leng=leng-1
else:
print(s1, "is a palindrome")
#alternate method
#by finding the reverse and the check whether both strings are same or not
rev= ""
for i in s1:
rev=i+rev
if s1==rev:
print(s1, "is a palindrome")
else:
print(s1, "is not a palindrome")
Calm. This is easy school-level Python, only strings, no fancy stuff. I’ll keep it simple + clean, exactly
exam-friendly.
Given string 👇
s = "Python is an interpreted, high-level and general-purpose programming language."
a) Find the length of the string
s = "Python is an interpreted, high-level and general-purpose programming language."
print(len(s))
b) Find the word "and" and replace it with "&"
s = "Python is an interpreted, high-level and general-purpose programming language."
s = [Link]("and", "&")
print(s)
c) Join the string with the text
"It has a simple syntax similar to the English language."
s = "Python is an interpreted, high-level and general-purpose programming language."
s2 = " It has a simple syntax similar to the English language."
result = s + s2
print(result)
📌 VERY IMPORTANT (exam tip)
len() → length
replace() → replace word
+ → join strings
Given list 👇
fruits = ['Orange', 'Mango', 'Papaya', 'Apple', 'Grapes']
a) Remove "Papaya" and add "Guava" at the same position
fruits = ['Orange', 'Mango', 'Papaya', 'Apple', 'Grapes']
pos = [Link]('Papaya')
[Link]('Papaya')
[Link](pos, 'Guava')
print(fruits)
📌 Explanation (tiny):
index() → finds position
remove() → deletes item
insert() → adds item at position
b) Sort the list in ascending order
[Link]()
print(fruits)
c) Add "Cherry, Banana, Coconut" to the list
[Link](['Cherry', 'Banana', 'Coconut'])
print(fruits)
🔑 One-line memory help
remove() → removes item
insert() → adds at position
sort() → alphabetical order
extend() → add many items