Skip to main content

GRADE XI - STRING PRACTICE QUESTIONS - 21 OCT 24

 

PYTHON STRINGS

PRACTICE QUESTIONS


1. FIND THE OUTPUT

x = "hello world"

print (x[:2], x[:-2], x[-2:])

print (x[6], x[2:4])

print (x[2:-3], x[-4:-2])

2.

x = "hello" + "Python" 

for chr in x :

    y = chr

    print (y, ":", end = ' ')


3.
str = "Mississippi"
for i in range (len (str)) :
    if str[ i ] == 'i' :
        print (i)

4.
st="LoTusValLey is my school"
ns=""
for i in range(1,len(st)//2):
    if st[i].isupper():
        ns=ns+st[i].lower()
    else:
        ns = ns+st[i].upper()
print(ns)  

5.
input='LotuSValLey'
output=""
for i in input:
    ans=ord(i)
    if ans>=65 and ans<=90:
        output+=chr(ans+32)
    else:
        output+=chr(ans-32)
print(input + ' after changing ' + output)


6.
s = '0123456789'
print(s[3])
print (s[3], s[0:3], '-', s [2:5])
print (s[:3], '-', s[3:], ',', s[3:100])
print (s[20:], s[2:1], s[1:1])

7.Consider the following code and answer the questions given in part a and b.
str1 = input("Enter string")
while len(str1)<=4:
    if str1[-1]=='z':
        str1=str1[0:3]+'c'
    elif'a' in str1:
        str1=str1[0]+'bb'
    elif not int(str1[0]):
        str1='I' + str1[1:]+'z'
    else: str1=str1+'*'
print(str1)
a. if the input is 1bzz.Also mention the number of times the code will be executed.
b. How many times the loop will be executed if the input is abcd

9. Find the output:
Text1="AIsScE2019"
Text2=""
i=0
while i< len(Text1):
    if Text1[i] >="0" and Text1[i] <"9":
        val=int(Text1[i])
        val=val+1
        Text2=Text2+str(val)
        
    elif Text1[i] >="A" and Text1[i] <="Z":
            Text2=Text2+(Text1[i+1])
            print(Text2)
    else:
        Text2=Text2+"*"
    i+=1
print(Text2)

PRACTICAL QUESTIONS

Q1. Write a program that prompts a user for their "password" and then determines if the password is valid or not. 
A password is said to be valid if it starts with a digit and it has length 6 or more. 
If your program determines that the user-entered password is not valid, it should print a message saying so. 
Otherwise, it should print a message saying that it has accepted the user-entered password.

Q2. Write a program that inputs a word and prints "Yes it starts !!" if the given word starts with "0", "1", "2", ... , "9".
sol:
str = input ("Enter the word :- ")
if str[0].isdigit() :
    print ("Yes it starts !!" )
else :
    print("The string does not start with 0, 1, 2, ... , 9")


Q2. Write code to print string in a toggle case.
sol:
String = 'LoTusValLey'
#initialize other empty String
String1 = str()
#iterate through String
for i in String:
    #check the case of each iterator
    if i.isupper():
        #change it to opposit case
        i = i.lower()
        #Concat each iterator to String1
        String1 = String1 + i
    else:
        i = i.upper()
        String1 = String1 + i
#print String1
print(String + ' after changing ' + String1)

Q3.Write a program that prompts the user for a string 's' and a character 'c', and outputs the string produced from s by capitalizing each occurrence of the character c in s and making all other characters lowercase.

For example, if the user inputs "Mississippi" and "s", the program outputs "miSSiSSippi".
Sol:

s = input ("Enter the word :- ")
c = input ("Enter the character :- ")
str = ""
for i in range (len (s)) :
    if s[ i ] == c :
        str += s[ i ].upper()
    else :
        str += s[ i ]
print (str)

Q4. Write a program to accept a string. Make a new string with all the consonants deleted from the string.
str1=input("Enter the string")
str2=""
for ch in str1:
    if ch not in 'aeiouAEIOU':
        str2+=ch
print("string without vowels",str2)









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...