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

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