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

GRADE XII - Python Connectivity with MySQL

  Python Connectivity with MySQL In real-life applications, the data inputted by the user and processed by the application needs to be saved permanently, so that it can be used for future manipulation. Usually, input and output data is displayed during execution but not stored, as it resides in main memory , which is temporary — i.e., it gets erased once the application is closed. This limitation can be overcome by sending the data to be stored in a database , which is made accessible to the user through a Front-End interface . Key Concepts Database A database is an organized collection of data that is stored and accessed electronically from a computer system. DBMS (Database Management System) A DBMS is software that interacts with end-users, applications, and the database to capture and analyze data. Front-End The Front-End is the user interface of the application, responsible for input/output interaction with the user. Back-End The Back-End refe...

GRADE XI | DESIGN THINKING & INNOVATION (848) | INTRO | CH-1

  DESIGN THINKING &  INNOVATION (848)  INTRODUCTION LINK : INTRODUCTION PPT PPT LINK Notes: Design Thinking and Innovation Grade XI Introduction Section   I. What is Design? (0.1.1) Definitions from Experts: John Maeda: "Design is solution to a problem". Saul Bass: "Design is thinking made visual". Charles Eames:   “Design is a plan for arranging elements in such a way as best to accomplish a particular purpose.” Steve Jobs:  "Design is not just what it looks like and feels like. Design is how it works". Prof. Sudhakar Nadkarni:  "Essentials of design are— purity, precision, details". Design is a way of understanding needs, identifying problems, and creating appropriate and innovative solutions. It is not only about appearance, but also about usefulness and sustainability. Design is explained as something that helps solve problems and make a positive difference. Key ...