GRADE XI
PYTHON STRINGS
Practice Questions
PPT LINK :PPT LINK
1. #Code to check number of words starting with Vowel (Without in-built function)
string = input("Enter a string: ")
length = len(string)
ctr = 0
i = 0 # Pointer to traverse the string
while i < length:
# Skip any extra spaces
while i < length and string[i] == " ":
i += 1
# check if it starts with a vowel
if i < length and string[i] in "aeiouAEIOU":
ctr += 1
# Move the pointer to the end of the current word
while i < length and string[i] != " ":
i += 1
print("Number of words starting with a vowel:", ctr)
With Split Function:
string = input("Enter a string: ")
words = string.split()
print("Words:", words)
ch = 0
for word in words:
# Check if the first letter of the word is a vowel
if word[0].lower() in 'aeiou':
ch += 1
print("Number of words starting with a vowel:", ch)
2.STRING PALINDROME
st = input("Enter a String: ")
flag=True
i = 0
j = len(st) - 1
while i <= j:
if(st[i] != st[j]):
flag=False
i += 1
j -= 1
if flag:
print("The given string",st,"is a palindrome")
else:
print("The given string",st,"is not a palindrome")
3.# Code to calculate the sum of digits in a string
str1 = "123abcd45"
print(str1)
# Initialize a variable to store the sum of digits
sum_digit = 0
for char in str1:
if char.isdigit():
digit = int(char)
sum_digit += digit
print(sum_digit)
4.Write a program to accept a string and convert all lower case letters to upper case.
str=input("enter String - ")
newstr = " "
for ch in str:
if ch >= "a" and ch <= "z":
newstr += chr(ord(ch)-32)
else:
newstr += ch
print("The converted String - ",newstr)
5. Write a menu driven program to accept a string and do the following operation on user ‘s choice : 1. Display no. of vowels 2. Check for palindrome 3. Reverse string
Comments
Post a Comment