Skip to main content

GRADE XI - PYTHON STRINGS WITH PDF NOTES

 

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


6. Find the output: String Slicing
>>> str = "Computer Science"
>>> print(str[3:7]) 
>>> print(str[4:]) 
>>> print(str[0:]) 
>>> print(str[:6]) 
>>> print(str[1:10:2])
>>> print(str[4::2]) 
>>> print(str[:10:2])
>>> print(str[10:2:-1]) 

7. Write a program to accept a string with multiple words.
Form an integer by extracting all digits from the string in the order they occurred.

Eg: enter string- 8irst3dig4sum5
sting entered- 8irst3dig4sum5
Extracted number as integer - 8345

str= input("enter string-")
num= 0
for ch in str:
    if ch.isdigit():
        num= int(ch) + num*10
print("sting entered",str)
print("Extracted number as integer -",num)


Comments

Popular posts from this blog

CS - SORTING/SEARCHING ALGORITHMS

  SORTING ALGORITHMS                       SORTING ALGORITHM PDF LINK #Bubble Sort          ·        The outer loop iterates through the entire array. ·        The inner loop compares adjacent elements and swaps them if they are out of order. ·        The outer loop runs n times, and each pass moves the largest element to its correct position. arr=[3,8,5,2,1] n = len(arr) print(n) for i in range(n):  #traverse through all the elements         # Last i elements are already sorted, no need to check them         for j in range(0, n-i-1):              # Swap if the element found is greater than the next element              if arr[j] > arr[j+1]:               ...

GRADE XI - NESTED FOR LOOP

                                                         NESTED FOR LOOP 1. for var1 in range(3):      print(var1,"OUTER LOOP")          # 0 outer loop       1 outer loop      2 outer loop          for var2 in range(2):                  print(var2+1,"INNER LOOP")    #1  2 inner loop     1  2  inner loop   1 2 inner loop  2. Print the following pattern using for loop: 1  1 2  1 2 3  1 2 3 4  Sol: for r in range(1,5):   #ROWS     for c in range(1,r+1):   #COLUMNS         print(c,end=" ")     print() 3. Print the following pattern using for loop: @  @ @  @ @ @...