Questions for practice:
Practice Questions of String in Python – Test 1
Q1. What is String in Python?
Q2. Is there any difference in ‘a’ or “a” in python?
Q3. Is there any difference between 1 or ‘1’ in python?
Q4. Python treats single quotes same as double quotes.(T/F)
Q5. A string with zero character is called __________ string.
Q6. Python does not support a character type.(T/F)
Q7. Write a code to store following String in a variable named ‘str’.
Q8. Write the output of the following code:
str1 = "Welcome to my blog"
str2 = "Welcome to my \n Blog"
print(str1)
print(str2)
Q9. Write the output of the following.
str1 = "Welcome \tto my Blog"
str2 = "Welcome to\n my \tBlog
print(str1)
print(str2)
Q10. Write the output of the following.
str1 = “”” Welcome to my
blog.
This is for
Class X”””
print(str1)
Practice Questions of String in Python – Test 2
Q1. Write the output of following code-
a) str="hello"
print(str[:3])
b) str=’My Blog’
a=’ ‘
for i in range(len(str)):
a+=str[i]
print(a)
c) str='MyBLog'
a=' '
for i in range(len(str)):
print(i * str[i])
d)
s='My'
s1='Blog'
s2=s[:1]+s1[len(s1)-1:]
print(s2)
e) print(“My”+’Blog’ * 2)
f) print(“My” *3 + “Blog” +’7′)
g) for i in range(2,7,2):
print(i * '2')
h) for i in range(3,12, 2):
print("a".upper())
i) for i in range(3,12,13):
print("a".upper)
j) s='Welcome to My Site [Link] '
print([Link]('come'))
print([Link]('o'))
print([Link]('o', 10, 20))
print([Link]('o', 5, 10))
Practice Questions of String in Python – Test 3
Q1. Write a code to create empty string 'str1'
Q2. What do you mean by traversing a string?
Q3. What is the index value of first element of a string?
Q4. What is the index value of last element of a string?
Q5. If the length of the string is 10 then what would be the positive index value
of last element?
Q6. If the length of string is 9, what would be the index value of middle
element?
Q7. Index value of a string can be in float. (T/F)
Q8. What type of error is returned by following statement, if the length of
string 'str1' is 10.
print(str1[13])
Q9. Write the output of the following:
str1 = "Welcome to my Blog"
a. print(str1[-1])
b. print(str1[9])
Q10. Write a code to assign a string "Hello World"' to a string variable named
"str1".
Practice Questions of String in Python – Test 4
Q1. Write a program to display each character of the following string in
separate line using 'for' loop.
str1 = Welcome to My Blog
Q2. Write a program to display each character of the following string in
separate line using 'while' loop.
str1 = Welcome to My Blog
Q3. Index value of string "str1" varies from 0 to len(str1)-1. (T/F)
Q4. Write the positive and negative index value of 'B' in the following string.
"Welcome to my Blog"
Q5. What do you mean by concatenation of string?
Q6. Which of the following is an example of concatenation?
a. 6 + 3
b. '6' + '3'
c. 'a' + 'b' + 'c'
Q7. Write a program to count the length of string without using inbuilt
function.
Q8. Write the output of the following statement.
str1 = "Welcome to my Blog"
for i in str1:
print(i)
print(i, end =' ')
print(i, end = '#')
Q9. Write the output of the following statement.
str1 = "Amit"
for i in str1:
print(i)
print(i, end =' ')
Q10. Write the output of the following statement.
str1 = "Welcome to my Blog"
for i in str1:
print(i)
print(i, end =' ')
print(i, end = '#')
Practice Questions of String in Python – Test 5
Q1. Match the Following
Column-1 Column-2
Slicing string[range] + ‘x’
Concatenation string[: – 1]
Repetition string[range]
Membership string1 + string2
Reverse in, not in
Q2. Write the code to join string 'str1' and 'str2' and store the result in variable
'str3'
Q3. The ___________ operator join the string.
Q4. What type of error is shown by following code?
>>> 2 + '3'
Q5. Which operator is used to replicate string?
Q6. Write the output of the following.
>>> 'A' * 3
Q7. Write the output of the following:
for i in range(2,5,2):
print(i * '@')
Q8. 'in' or 'not in' are the _________ operator
Q9. Write the output of the following
>>> 'h' in 'Hindi'
>>>'i' in 'Hindi'
Q10. Write the output of the following
for i in "Amit":
print(i in "Amit")
Practice Questions of String in Python – Test 6
Q1. Write the output of the following
>>>"string" in "substring"
>>>"string" not in "substring"
Q2. Write the output of the following:
>>>'tie' == 'ties'
>>>"Amit" != "Amitabh"
>>>"amit" > "Amitabh"
Q3. Write the full form of ASCII
Q4. Write the ASCII value of 'A' and 'a'.
Q5. String Slicing is used to fetch substring from a string. (T/F)
Q6. Write the output of the following.
S = "Welcome to my Blog"
print(S[2 : 3])
print(S[2 : 10])
print(S[-2 : ])
print(S[-10 : -2 : 2])
Q7. Strings are mutable.(T/F)
Q8. Write the output of the following
>>>str1 = "Anita"
>>>str1[1] = 'm'
>>>print(str1)
Q9. Name any two built in function associated with string.
Q10. Which function of string return the numerical value?
Practice Questions of String in Python – Test 7
Q1. What is the value returned by find() function for an unsuccessful search?
Q2. isalpha() returns True if the string contains only alphabet.(T/F)
Q3. Write the output of the following
str1 = "Amit"
str2 = "My Blog"
str3 = "#blog"
str4 = "My 1st Blog"
print([Link]())
print([Link]())
print([Link]())
print([Link]())
Specify the reason if any of the above print statement return False.
Q4. Write a program to accept string and display total number of alphabets.
Q5. Write a program to accept a string and display the sum of the digits, if any
present in string.
for example:
input string : My position is 1st and my friend come on 4th
output : 5
Q6. Write the output of the following:
>>>a = 123
>>>b = "123"
>>>[Link]()
>>>[Link]()
Q7. What is the difference between lower() and islower()?
Q8. Write a program to accept a string and convert it into lowercase.
Q9. Write a program to count the number of lowercase and uppercase
character in a
string accepted from the user.
Q10. Write the output of the following:
>>>s = "My blog"
>>>[Link]()
>>>[Link]()
>>>[Link]()
>>>[Link]()
>>>[Link]()
>>>[Link]()
Practice Questions of String in Python – Test 8
Q1. What is lstrip () function in String?
Q2. Write the output of the following.(# represents the spaces)
>>>str1 = "Welcome to my Blog"
>>>print(len(str1))
>>>str2 = "###Welcome to my Blog"
>>>print(len(str2))
>>>print(len([Link]()))
Q3. Write the output of the following.(# represents the spaces)
>>>str2 = "###Welcome to my Blog####"
>>>print(len(str2))
>>>print(len([Link]()))
>>>print(len([Link]()))
Q4. Write the output of the following.
>>>str1 = "Welcome to my Blog"
>>>print([Link]('og'))
>>>print([Link]('We'))
>>>print([Link]('Welog))
Q5. Write the output of the following.
>>>print(len([Link]('og')))
>>>print(len([Link]('We')))
>>>print(len([Link]('Welog)))
Q6. isspace() returns True if the string contain only spaces (T/F)
Q7. Write the output of the following
>>>s1 = " "
>>>s2 = " Amit"
>>>print([Link]())
>>>print([Link]())
Q8. Define the following function with example
a. istitle()
[Link]()
Practice Questions of String in Python - Test 9
Q1. Write the output of the following:
str1 = "Welcome to my Blog"
a. print(len(str1))
b. print(capitalize(str1))
Q2. Write the output of the following.
>>>str1 = "Welcome to my Blog"
>>>x = [Link]()
>>>print(x)
Q3. Write a program to accept a string and display each word and it's length.
Q4. Write a program to accept a string and display string with capital letter of
each word.
for example, if input string is : welcome to my blog
output string : Welcome To My Blog
Q5. What is split() function in String?
Q6. Write the output of the following:
a = "Mummy?Papa?Brother?Sister?Uncle"
print([Link]())
print([Link]('?')
print([Link]('?',1)
print([Link]('?',3)
print([Link]('?',10)
print([Link]('?',-1)
Q7. Write a program to replace all the word 'do' with 'done' in the following
string.
str1 = "I do you do and we all will do"
str1 = "I do you do and we all will do"
l=[Link]()
for i in range(len(l)):
if l[i]=="do":
l[i]="done"
s=" "
for i in l:
s=s+" "+str(i).strip()
print(s)
Q8. Write the output of the following.
str1 = "I went to Auli"
print([Link]("Auli", "Leh"))
print(str1)
Q9. What is the purpose of find() function in string?
Q10. Write the output of the following:
str1 = "Welcome to my Blog"
print([Link]('o'))
print([Link]('o',3))
print([Link]('o',7))
print([Link]('o',7,10))
ADDITIONAL PRACTICE QUESTIONS
Q1. Write a program to accept a string and display the string with second
alphabet of each word in upper case.
str = input("Enter any String: ")
str1 = ""
L = [Link]()
for i in L:
str1 = str1 + i[0] + i[1].upper() + i[2:] + " "
print(str1)
Q2. Write a program to accept a string and display the word which has vowel
‘a’ in it.
str = input("Enter any String: ")
L = [Link]()
for i in L:
if 'a' in i:
print(i)
Q3. Write a program to accept the first name from the user and display it as
many times as its length.
str = input("Enter first name: ")
for i in range(len(str)):
print(str)
Q4. Write a program to accept any string from the user and display it in the
following pattern. If the string entered is “Python String Programs”, the output
should be: Python / Python String / Python String Programs
str = input("Enter any string: ")
L = [Link]()
str1 = ""
for i in L:
str1 = str1 + i + " "
print([Link]())
Q5. Write a program to accept a string from the user and display the string
without spaces.
str = input("Enter any string: ")
str1 = ""
for i in str:
if not [Link]():
str1 = str1 + i
print(str1)
Q6. Write a program to accept a string and substring from the user and display
the frequency of substring in string.
str = input("Enter any string: ")
substr = input("Enter any substring: ")
print([Link](substr))
Q7. Write a program to replace all ‘a’ in a string by symbol ‘@’.
str = input("Enter any string: ")
str1 = ""
for i in str:
if i != 'a':
str1 = str1 + i
else:
str1 = str1 + '@'
print(str1)
Q8. Write a program to accept a string and substring from the user and check
whether the substring is present or not.
str = input("Enter any string: ")
substr = input("Enter any substring: ")
if [Link](substr) != -1:
print("Substring is present")
else:
print("Substring is not present")
Q9. Write a program to accept a string from the user and display n characters
from the right of the string.
str = input("Enter any string: ")
n = int(input("Enter number of characters to extract from right: "))
L = len(str)
if n > L:
print("Enter smaller number of characters")
else:
print(str[-n:])
Q10. Write a program to accept a word from the user and display it in the
following pattern. If the word is “river”, it should display:
r
ri
riv
rive
river
str = input("Enter any string: ")
str1 = ""
for i in str:
str1 = str1 + i
print(str1)
Practice Questions of String in Python – Test 10
Q1. Accept a string and display in reverse order.
Ans.
str = input("Enter any String")
print(str[: : -1])
Q2. Write a program to accept a string and display 20 times.
Ans.
str = input("Enter any String")
for i in range(20):
print(str)
Q3. Write a program to accept a string in python and display the entire string in
upper case.
Ans.
str = input("Enter any String")
print([Link]())
Q4. Write a program to accept a string and display last three characters of the
string.
Ans.
str = input("Enter any String")
print(str[-3 : : 1])
Q5. Write a program to accept a string in python and display the entire string in
lower case.
Ans.
str = input("Enter any String")
print([Link]())
Q6. Accept a string and display the entire string with first and last character in
upper case.
Ans.
str = input("Enter any String")
print(str[0].upper() + str[1:-1] + str[-1].upper())
Q7. Write a program to accept a string and display first three characters of the
string.
Ans.
str = input("Enter any String")
print(str[0:3])
Q8. WAP which accepts a string and display total number of vowels.
Ans.
v=0
for i in range(len(str)):
if str[i] in "aeiouAEIOU":
v = v +1
print("Total number of vowels are ", v)
Q9. WAP which accepts a string and display total number of lower case
characters.
Ans.
l=0
for i in range(len(str)):
if str[i].islower( ):
l = l +1
print("Total number of lower case are ", l)
Q10. WAP which accepts a string and display total number of digits.
Ans.
d=0
for i in range(len(str)):
if str[i].isdigit( ):
d = d +1
print("Total number of digits are ", d)