Worksheet class 11 string
Worksheet class 11 string
s = "Python"
print(s[1:4])
a) Pyt b) yth c) tho d) ytho
Answer: b) yth
2. Which of the following methods is used to remove any whitespace from the beginning or
the end of a string?
a) strip() b) split() c) replace() d) find()
Answer: a) strip()
s = "hello"
print(s.upper())
a) HELLO b) hello c) error d) hELLO
Answer: a) HELLO
4. Which of the following is not a valid string method in Python?
a) upper() b) reverse() c) split() d) join()
Answer: b) reverse()
"Python" * 2
Answer: c) PythonPython
def is_palindrome(s):
s = s.lower()
return s == s[::-1]
def count_vowels(s):
vowels = "aeiouAEIOU"
count = 0
for char in s:
if char in vowels:
count += 1
return count
def reverse_string(s):
return s[::-1]
def replace_spaces(s):
return s.replace(" ", "_")
5. Write a Python program to find the length of a string without using the len()
function.
Answer:
def string_length(s):
count = 0
for char in s:
count += 1
return count
s = "Python Programming"
print(s[::2])
Answer: d) PthnPormig
def starts_with_vowel(s):
vowels = "aeiouAEIOU"
return s[0] in vowels
s = "Python"
print(s[-6])
a) n b) P c) y d) h
Answer: b) P
2. Which method is used to return a copy of the string with leading and trailing whitespaces
removed?
a) rstrip() b) strip() c) lstrip() d) replace()
Answer: b) strip()
s = "abcdef"
print(s.index("e"))
a) 3 b) 4 c) 5 d) Error
Answer: b) 4
"Python" + "Rocks"
Answer: a) PythonRocks
Answer: a) s.lower()
python
Copy code
def are_anagrams(str1, str2):
return sorted(str1) == sorted(str2)
2. Write a Python program to remove all special characters from a given string.
Answer:
import re
def remove_special_chars(s):
return re.sub(r'[^A-Za-z0-9 ]+', '', s)
def char_frequency(s):
freq = {}
for char in s:
if char in freq:
freq[char] += 1
else:
freq[char] = 1
return freq
def reverse_words(sentence):
words = sentence.split()
return ' '.join(reversed(words))