Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

String in Python

The document contains a series of questions and short answers related to string operations in Python. It covers various string methods, their outputs, and programming exercises to manipulate strings. The content is structured as a quiz format, testing knowledge on string functionalities.

Uploaded by

Vishal .U.S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

String in Python

The document contains a series of questions and short answers related to string operations in Python. It covers various string methods, their outputs, and programming exercises to manipulate strings. The content is structured as a quiz format, testing knowledge on string functionalities.

Uploaded by

Vishal .U.S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Strings in Python

Questions:
1. What is the output of print("Hello"[1])?

• (a) H
• (b) e
• (c) l
• (d) o
2. Which of the following methods can be used to convert a string to lowercase?
• (a) lower()
• (b) islower()
• (c) upper()
• (d) capitalize()
3. What will len("Python Programming") return?

• (a) 18
• (b) 17
• (c) 16
• (d) 15
4. What will be the output of print("Python".upper())?

• (a) python
• (b) PYTHON
• (c) Python
• (d) None
5. What does str.isdigit() return for "123abc"?

• (a) True
• (b) False
• (c) Error
• (d) None
6. Which of the following methods splits a string into a list by whitespace by default?
• (a) join()
• (b) split()
• (c) partition()
• (d) slice()
7. What will be the result of "Python"[-1]?

• (a) P
• (b) n
• (c) h
• (d) o
8. What does "hello".capitalize() do?

• (a) Converts all letters to uppercase


• (b) Converts only the first letter to uppercase
• (c) Converts only the first letter to lowercase
• (d) None of these
9. What is the result of "Hello" * 3?

• (a) HelloHelloHello
• (b) Hello3
• (c) Error
• (d) None of these
10.What does "Python".startswith("Py") return?

• (a) True
• (b) False
• (c) None
• (d) Error
11.Which method removes trailing spaces from a string?
• (a) strip()
• (b) lstrip()
• (c) rstrip()
• (d) trim()
12.What does "Python".find("o") return?

• (a) 3
• (b) 4
• (c) -1
• (d) Error
13.Which of the following will concatenate "Hello" and "World"?

• (a) "Hello".concat("World")
• (b) "Hello" + "World"
• (c) "Hello".append("World")
• (d) "Hello".join("World")
14.How can you replace all occurrences of "a" with "o" in "banana"?

• (a) replace("a", "o")


• (b) replace("o", "a")
• (c) replace("a", "o", 1)
• (d) replaceall("a", "o")
15.What will be the result of "Hello".islower()?

• (a) True
• (b) False
• (c) Error
• (d) None
16.Which function converts a string to a list of characters?
• (a) split()
• (b) list()
• (c) join()
• (d) partition()
17.What will "abcd".isalpha() return?

• (a) True
• (b) False
• (c) Error
• (d) None
18.What is the purpose of join() method?

• (a) Combine strings in a list into a single string


• (b) Split a string into a list of substrings
• (c) Replace characters in a string
• (d) Find substrings in a string
19.How can you check if "12345" contains only numeric values?

• (a) isnum()
• (b) isdigit()
• (c) isnumber()
• (d) isalnum()
20.Which method would you use to convert a string " Python " to "Python"?

• (a) trim()
• (b) strip()
• (c) rstrip()
• (d) lstrip()
21.Which of the following string operations raises an error?
• (a) "hello" + "world"
• (b) "hello" * 2
• (c) "hello" - "world"
• (d) "hello".upper()
22.What will "Python Programming".count("P") return?

• (a) 1
• (b) 2
• (c) 0
• (d) Error
23.What is the result of "Hello".swapcase()?

• (a) HELLO
• (b) hello
• (c) hELLO
• (d) None
24.How do you check if a string starts with "Py"?

• (a) startswith("Py")
• (b) startwith("Py")
• (c) begin("Py")
• (d) start("Py")
25.What is the result of len(" ")?

• (a) 0
• (b) 1
• (c) 2
• (d) Error
26.Which method would you use to convert "PYTHON" to "Python"?

• (a) capitalize()
• (b) title()
• (c) lower()
• (d) upper()
27.How can you find the number of occurrences of "a" in "banana"?

• (a) count("a")
• (b) find("a")
• (c) index("a")
• (d) search("a")
28.What will "hello world".replace("world", "Python") return?

• (a) hello Python


• (b) hello worldPython
• (c) hello Pythonworld
• (d) hello Python!
29.Which method splits a string into three parts based on the first occurrence of a substring?
• (a) split()
• (b) partition()
• (c) rpartition()
• (d) slice()
30.What does "hello".endswith("o") return?

• (a) True
• (b) False
• (c) None
• (d) Error

Short Answers:

1. Define String
2. What is the purpose of join?
3. What is the difference between find() and index() function of string?
4. What is the difference between split() and partition() function of string?
5. Given string : s = ‘Friendliness’ . Write the output for the following.
(i) s[: 3]
(ii) s[5: 10]
(iii) s[-6: 11]
(iv) s[2: -3]
(v) s[6:]
(vi) s[::-1]
(vii) s[2:10:2]
(viii) s[8:1:-1]
(ix) s[-3:2:-2]
(x) s[::3]

5. Given string : s = ‘there is a CAR’ . Write the output for the following.
(a) s.upper()
(b) s.lower()
(c) s.title()
(d) s.capitalize()
(e) s.split()
(f) s.split(‘e’)
(g) s.parittion(‘r’)
(h) s.count(‘a’)
(i) s.find(‘b’)
(h) s.index(‘e’, 3)

6. Give string: s = “Single Job”. Write python statement for the following.
(I) display all the characters in Uppercase.
(ii) display the string in titled case.
(iii) get the words of the string in list
(iv) display the first five characters
(v) display last 3 characters in reverse.

7. Write a python program to get a string as input and prints all the character separated by #.

8. Write a python program to get a string as input and print all the words in separate lines.

9. Write a python program to get a string as input and print all the words that begin with vowel.

10. Write a python program to get a string as input and print all the three lettered words.
11. Write a python program to get a string as input and print the words that begin and end with same
letter.

12. Write a python program to get a string as input and print whether it is a palindrome or not.

13. Write a python program to get a string as input and count the letter ‘t’ and ‘s’ in it.

15. Write a python program to get a string as input and print the words that has the letter ‘a’ in it.

16. Write a python program to get a string as input and print the words that has more 5 characters.

17. Write a python program to get a string as input and count and print the no of vowels, consonants,
uppercase, lowercase, digits and spaces available in the string.

18. Write a python program to get string as input and print the words that have 2 or more ‘a’ in it.

19. Write a python program to get a string as input and print the strings with words printed in reverse
order.

20. Write a python program to get a string as input and print the palindrome words from the string.

21. Write the output for the following program


s="Welcome2cs"
n=len(s)
m=""
for i in range(0,n):
if(s[i]>='a' and s[i]<='m'):
m+=s[i].upper()
elif(s[i]>='n' and s[i]<='z'):
m+=s[i-1]
elif(s[i].isupper()):
m+=s[i].lower()
else:
m+='&'
print("The string is: ",m)

22. Write the output for the following program

mystr = 'alPHa123'
newstr = ""
count=0
for i in mystr:
if count % 2 != 0:
newstr = newstr + str(count)
else:
if i.lower():
newstr = newstr + i.upper()
else:
newstr = newstr + i
count += 1
print(newstr)

23. Write the output for the following

Text1 = "IND23"
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])
else:
Text2 = Text2 + "*"
I += 1

print(Text2)

You might also like