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

PYTHON - MYSQL CONNECTIVITY CODE

  #INSERTION OF DATA import mysql.connector mydb = mysql.connector.connect( host="localhost", user="root", passwd="root", database="school" ) print("Successfully Connected") #print(mydb) mycursor=mydb.cursor()   v1=int(input("enter ID:")) v2=input("enter name:") v3=input("enter Gender:") v4=int(input("enter age:")) sql='insert into TEACH values("%d","%s","%s","%s")'%(v1,v2,v3,v4) print(sql) mycursor.execute(sql) mydb.commit() print("record added") #MYSQL Connection code – Deletion on database SOURCE CODE: s=int(input("enter id of TEACHER to be deleted:")) r=(s,) v="delete from TEACH where id=%s" mycursor.execute(v,r) mydb.commit() print("record deleted") MYSQL Connection code – Updation on database SOURCE CODE: import mysql.connector mydb = mysql.connector.c...

REVISION IF CONSTRUCT | CLASS TEST

                                                                                     CLASS TEST 1. Write a Python program that asks the user for their age, gender, and current fitness level (beginner, intermediate, or advanced). Based on this information, suggest a suitable fitness plan using if-else statements. Requirements: Inputs : Age (integer) Gender (male/female) Fitness level (beginner/intermediate/advanced) Outputs : Recommend a fitness plan that includes: Suggested workout duration. Type of exercises (e.g., cardio, strength, flexibility). Rest days. Logic : Use if-else to determine the plan based on conditions such as: Age group (e.g., <18, 18–40, >40). Fitness leve...