In this post I am going to share Top 30 Question and Answer Python String, can be useful for CBSE Class XI and XII computer science with Python subject.
Top 30 Question and Answer Python String
Q1. What is String in Python?
Ans. A sequence of characters which are enclosed in single(‘ ‘) or double (“”) quotes is known as String.
Q2. Is there any difference in ‘a’ or “a” in python?
Ans. No.
Q3. A string with zero character is called __________ string.
Ans. Empty.
Q4. Write the output of the following.
str=”hello”
print(str[:4])
Ans. Hell
Q5. What do you mean by traversing a string?
Ans. Traversing a string means accessing all the elements of the string one by one by using index value.
Q6. What is the index value of the first and last element of a string?
Ans. First 0 and last -1.
Q7. If the length of the string is 10 then what would be the positive index value of the last element?
Ans. 9 (10-1)
Q8. What type of error is returned by the following statement, if the length of string ‘str1’ is 10.
print(str1[13])
Ans. Index error.
Q9. Write the output of the following:
str1 = “Welcome to my Blog”
- print(str1[-1])
- print(str1[9])
Ans. 1. G and 2.o
Q10. Which of the following is an example of concatenation?
- 6 + 3
- ‘6’ + ‘3’
- ‘a’ + ‘b’ + ‘c’
Ans. 1 and 3
Q11. What type of error is shown by the following code?
>>> 2 + ‘3’
Ans. TypeError
Q12. Write the output of the following.
>>> ‘A’ * 3
Ans. AAA
Q13. Write the output of the following
>>> ‘h’ in ‘Hindi’
>>>’i’ in ‘Hindi’
Ans. False , True
(‘h’ and ‘H’ are different)
Q14. Write a program to accept strings and display the total number of alphabets.
Ans.
str = input(“Enter any string”)
s=0
for i in str:
if i.isalpha()==True:
s = s+ 1
print(“Total number of alphabets are”, s)
Q15. What is the difference between lower() and islower()?
Ans. lower() converts a string to lowercase while islower checks a string whether it’s in lowercase or not.
Q16. Write a program to accept a string and convert it into lowercase.
Ans.
str = input(“Enter any string”)
print(str.lower())
Q17. Write the output of the following:
>>>s = “My blog”
>>>s.upper()
>>>s.lower()
>>>s.islower()
>>>s.isupper()
>>>s.isalpha()
>>>s.isdigit()
Ans.
MY BLOG
my blog
False
False
False
False
Q18. Which of the following is/are not legal string operators?
- in
- +
- *
- /
Ans. 4
Q19. Which of the following functions will return the total number of characters in a string?
- count()
- index()
- len()
- all of these
Ans. 3
Q20. Which of the following functions will return the first three characters of a string s?
- s[3:]
- s[:3]
- s[-3:]
- S[:-3]
Ans. 2 s[:3]
Q21. Which of the following functions will return the string with every ‘P’ replaced with a ‘z’?
- find()
- index()
- replace()
- split()
Ans. 3. replace()
Q22. Which of the following functions will return a list containing all words of the string?
- find()
- index()
- partition()
- split()
Ans. 4. split()
Q23. What is the output of the following code?
str1 = “Mission 999”
str2 = “999”
print(str1.isdigit(),str2.isdigit())
Ans. False True
Q24. Choose the correct function to get the ASCII code of a character.
- char(‘char’)
- ord(‘char’)
- ascii(‘char’)
- All of these
Ans. 2. ord(‘char’)
Q25. Given , which of the following expressions will return ‘How’ ?
- s[7:9]
- s[7:10]
- s[8:11]
- S[6:9]
Ans. B.
Q26. Which method should I use to convert String “Python programming is fun” to “Python Programming Is Fun” ?
- capitalize()
- title()
- istitle()
- upper()
Ans. 2. title()
Q27. Guess the correct output of the following String operations.
str1 = ‘Wah’
print(str1*2)
- WahWah
- TypeError: unsupported operand type(s) for * : ‘str’ and ‘int’
- WWaahh
- Wah2
Ans. 1. WahWah
Q28. If [‘one’, ‘four’, ‘three’, ‘two’]</code> is a list object, the <code>sort() function will return _____.
- [‘one’, ‘two’, ‘three’, ‘four’]
- [‘four’, ‘three’, ‘two’, ‘one’]
- [‘four’, ‘one’, ‘three’, ‘two’]
- None of the above
Ans. 3
Q29. To check whether string s1 contains another string s2, use __.
a) s1.contains(s2)
b) s2 in s1
c) s1.contains(s2)
d) si.in(s2)
Ans. a
Q30. Python does not support a character type; a single character is treated as strings of length one.
a) True
b) False
Ans. True.
Top 30 Question and Answer Python String query or suggestion
If you have any queries or suggestion regarding Computer Science with Python String Question and Answer then please leave a comment, I will try to answer as soon as possible.
If you have and query or suggestion then please comment, I will reply to fix your problem, if you like our content then you can subscribe to our Youtube channel. If you want to hire me then reach us at our Fiverr.
Thanks Sir, I am looking for it , can you provide pdf ….
I will try to upload a pdf for this post also