Python String Manipulation - W1
Python String Manipulation - W1
s = "Hello, World!"
print(s[7:])
A) Hello
B) World!
C) , World!
D) Hello,
s = "Python Programming"
print(s[:6] + " is fun")
A) Python is fun
B) Python Programming is fun
C) Programming is fun
D) thon is fun
Q3. Which of the following methods is used to remove whitespace from the beginning
and end of a string?
A) strip()
B) lstrip()
C) rstrip()
D) trim()
s = "banana"
print(s.count("a"))
A) 1
B) 2
C) 3
D) 4
A) s.toUpper()
B) s.upper()
C) s.uppercase()
D) uppercase(s)
s = "abcd"
print(s * 3)
A) abcdabcdabcd
B) abcd * 3
C) abcd abcd abcd
D) Error
Q7. Which of the following will correctly check if a string ends with a specific
substring?
A) s.ends_with("txt")
B) s.endswith("txt")
C) endswith(s, "txt")
D) s.endswiths("txt")
s = "hello world"
print(s.replace("world", "Python"))
A) Replaces all occurrences of "hello" with "Python"
B) Replaces "world" with "Python"
C) Replaces "Python" with "world"
D) Throws an error
Q9. Which of the following will convert the first character of each word in the string s =
"hello world" to uppercase?
A) s.toTitle()
B) s.capitalize()
C) s.title()
D) title(s)
s = "abracadabra"
print(s.find("ra"))
A) 2
B) 4
C) 7
D) 5
s = "Hello, World!"
print(s[7:])
A) Hello
B) World!
C) , World!
D) Hello,
s = "Python Programming"
print(s[:6] + " is fun")
A) Python is fun
B) Python Programming is fun
C) Programming is fun
D) thon is fun
Q13. Which of the following methods is used to remove whitespace from the beginning
and end of a string?
A) strip()
B) lstrip()
C) rstrip()
D) trim()
s = "banana"
print(s.count("a"))
A) 1
B) 2
C) 3
D) 4
A) s.toUpper()
B) s.upper()
C) s.uppercase()
D) uppercase(s)
s = "abcd"
print(s * 3)
A) abcdabcdabcd
B) abcd * 3
C) abcd abcd abcd
D) Error
Q17. Which of the following will correctly check if a string ends with a specific
substring?
A) s.ends_with("txt")
B) s.endswith("txt")
C) endswith(s, "txt")
D) s.endswiths("txt")
python
Copy code
s = "hello world"
print(s.replace("world", "Python"))
Q19. Which of the following will convert the first character of each word in the string s
= "hello world" to uppercase?
A) s.toTitle()
B) s.capitalize()
C) s.title()
D) title(s)
s = "abracadabra"
print(s.find("ra"))
A) 2
B) 4
C) 7
D) 5
III. Solve the following String Slicing and String Function Questions:
5. Write a program to accept a string input from the user and print:
10. Write a program to count the number of vowels in a given string. The program should:
12. Create a program to check if a string contains only alphabets (isalpha()), only digits
(isdigit()), or alphanumeric characters (isalnum()).
13. Write a program to count the number of uppercase and lowercase letters in a string.
15. Write a function remove_duplicates(s) that removes duplicate characters from a string
and returns the result. For example:
Input: "banana"
Output: "ban"
**************************************************************