String in Python
String 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) 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) 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) 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) 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.
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)
Text1 = "IND23"
Text2 = ""
I=0
print(Text2)