0% found this document useful (0 votes)
152 views

String

This document contains a worksheet on string manipulation in Python with 14 questions and solutions. It covers string operations like concatenation, slicing, indexing, counting characters/words/vowels/spaces in a string, checking for palindromes, capitalization functions like title(), upper(), capitalize() and more. Programs are provided to capitalize every other letter in a string, count occurrences of a substring, and check for palindromes. Appropriate Python functions are suggested for tasks like checking if a string contains digits, finding substrings, capitalizing the first letter, and removing leading whitespace.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
152 views

String

This document contains a worksheet on string manipulation in Python with 14 questions and solutions. It covers string operations like concatenation, slicing, indexing, counting characters/words/vowels/spaces in a string, checking for palindromes, capitalization functions like title(), upper(), capitalize() and more. Programs are provided to capitalize every other letter in a string, count occurrences of a substring, and check for palindromes. Appropriate Python functions are suggested for tasks like checking if a string contains digits, finding substrings, capitalizing the first letter, and removing leading whitespace.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

DELHI PUBLIC SCHOOL, AZAAD NAGAR, KANPUR

CLASS: XI,SUB: Computer Science CODE: 083


Chapter – STRING MANIPULATION- Worksheet
Q.1 which of the following is not a Python legal string operation? Q11.Write a program that reads a string and prints a string that capitalizes every
(a)‟abc‟ + ‟abc‟ (b) “abc‟*3 (c)‟abc‟ + 3 other letter in the string example student becomes StUdEnT
Q12.Write a program that reads a string and count :
Q.2 Out of the following operators, which ones can be used with strings? a. Number of space in given string b. Number of Vowels in given string
=, -, *, /, //, %, >, <>, in, not in, <= c. Number of words in given string d. Number of characters in given string
Q.3 From the string S = “CARPE DIEM”. Which ranges return “DIE” and “CAR”? Q13. What would following expression return?
1.Word =’amazing’ 2. Print 3* “go!” Differentiate between
Q.4 Given a string S = “CARPE DIEM”. If n is length/2 then what would be >>> Word[:4] , Word[4:] Print 5 *”@” 1.upper() and isupper()
following return? >>> Word[1:6:2] , Word[2:6:2] Print ‘3’ +’7’ 2.title() and capitalize()
(a) S [: n] b) S[n:] (c) S[n:n] (d) S [1:n] (e) S[n: length-1] >>> Word[-7:-3,3] , Word[::-2] Print ‘Hello’ +’Student ‘ 3.isalpha() and isalnum()
>>> Word[::-1] , Word[4:6] 3. 4.strip() and split()
Q.5 Find the errors - s=” PURA VIDA” Print(s[9] + s[9:15]) >>> Word[-3:-1] , Word[-4:-3] Print “abc” ==”abc”
Q6. >>>ord(‘a’),ord(‘z’) Print “ABC”==”abc”
>>>ord(‘A’),ord(‘Z’) Print “ABC”!=”abc”
What would following expression return? >>> chr(65),chr(90)
(a) ”HelloWorld”. Upper().lower() (b) ”Hello World”.lower().upper() Q14.Find the output:
( c) ”HelloWorld”. Find(“Wor”,1,6) (d) ”Hello World”. Find(“Wor”)
(e) ”Hello World”. Find(“wor”)
(f) ”Hello World”. isalpha() (g) ”Hello World”.isalnum() 1. 2.
(h) ”Hello World”.isdigit() (i) “123FGH”.isdigit() 3.

Q7.Suggest appropriate functions for the following tasks –


(a) To check whether the string contains digits.
(b) To find the occurrence a string within another string.
(c ) To convert the first letter of a string to upper case.
(d) To convert all the letters of a string to upper case.
(e) To check whether all the letters of the string are in capital letters.
(f) To remove all the white spaces from the beginning of a string.
Q8. Find the output – if we give input as “Hello” 4.

Q9.WAP to check the given string is palindrome or not. E.g. NITIN


Palindrome[rev] is: NITIN
Q10 WAP to print the number of occurrences of a substring into a line.
5. 6.
DELHI PUBLIC SCHOOL, AZAAD NAGAR, KANPUR
CLASS: XI,SUB: Computer Science CODE: 083
Q7.Suggest appropriate functions for the following tasks –
Chapter – STRING MANIPULATION- Worksheet Solutiom (c) To check whether the string contains digits.
Q.1 which of the following is not a Python legal string operation? (d) To find the occurrence a string within another string.
(a)‟abc‟ + ‟abc‟ (b) “abc‟*3 (c)‟abc‟ + 3 (c ) To convert the first letter of a string to upper case.
c) “abc” + 3 (d) To convert all the letters of a string to upper case.
(e) To check whether all the letters of the string are in capital letters.
Q.2 Out of the following operators, which ones can be used with strings?
=, -, *, /, //, %, >, <>, in, not in, <= (f) To remove all the white spaces from the beginning of a string.
*, + (a) isdigit () (b) find() (c) capitalize ()
(d) upper() (f) isupper() (g) lstrip()
Q.3 From the string S = “CARPE DIEM”. Which ranges return “DIE” and “CAR”?
Q8. Find the output – if we give input as “Hello”
S[6:9] for ―DIE and S[0:3] for ―CAR

Q.4 Given a string S = “CARPE DIEM”. If n is length/2 then what would be


following return?
(a) S [: n] b) S[n:] (c) S[n:n] (d) S [1:n] (e) S[n: length-1]
(a) “CARPE (b) “DIEM” (c) “ “ (d) “ARPE” (e)
“DIE”
Q.5 Find the errors - s=” PURA VIDA” Print(s[9] + s[9:15])
Here the error is : Sting index out of range.

Q6. Q9.WAP to check the given string is palindrome or not. E.g. NITIN
Palindrome[rev] is: NITIN
What would following expression return? s="NITIN"
(b) ”HelloWorld”. Upper().lower() (b) ”Hello World”.lower().upper() s1=s[::-1]
( c) ”HelloWorld”. Find(“Wor”,1,6) (d) ”Hello World”. Find(“Wor”) if s==s1:
print("String is pallindrome")
(e) ”Hello World”. Find(“wor”) else:
(f) ”Hello World”. isalpha() print("String is not pallindrome")
(g) ”Hello World”.isalnum()
(h) ”Hello World”.isdigit() (i) “123FGH”.isdigit() Q10 WAP to print the number of occurrences of a substring into a line.
s="johny johny yes papa, johny johny yes papa"
z=input("Enter a substring")
Ans: (a) 'hello world' (b) 'HELLO WORLD'
s1=s.split()
(c) -1 (d) 6 print(s1)
(e) -1 (f) False for i in range(len(s1)):
if s1[i]==z:
(g) False (h) False
c+=1
I) False print("Total no.of SubString is",c)
the uppercased string from the “True” if all the characters in the
given string. It converts all string are uppercase; otherwise, It
lowercase characters to the returns “False.”
Q11.Write a program that reads a string and prints a string that capitalizes every uppercase.
other letter in the string example student becomes StUdEnT EG: EG:
s="student" a = 'we are in the endgame a = 'WE ARE IN THE ENDGAME
for i in range(len(s)): now' NOW#'
if i%2==0: print(a. upper()) print(a.isupper())
z=s[i].capitalize()
print(z,end=" ")
else: Output Output
print(s[i],end=" ") WE ARE IN THE ENDGAME True
Q12.Write a program that reads a string and count : NOW
b. Number of space in given string b. Number of Vowels in given string
c. Number of words in given string d. Number of characters in given string title () capitalize ()
s="johny johny yes papa johny johny yes papa" title() function in Python is In Python, the capitalize() method
s1=s.split() the Python String Method converts the first character of a string
print("String is: ",s) which is used to convert the to a capital (uppercase) letter. If the
for i in range(len(s)): first character in each word to string has its first character as capital,
if s[i]==" ": Uppercase and remaining then it returns the original string.
space+=1 characters to Lowercase
if s[i] in'aeiouAEIOU': in the string and returns a
vowels+=1 new string.
EG: EG:
print("Total no. of spaces",space)
str1 = 'learning pYTHon iS str1 = 'learning pYTHon iS easy'
print("Total no. of vowels",vowels)
easy' str2 = str1.capitalize()
print("Total no. of characters",len(s)-space) str2 = str1.title() print ( str2)
print("Total no. of words",len(s1)) print ( str2)
Q13. What would following expression return? Output Output
1.Word =’amazing’ 2. print(3* "go!") Differentiate Learning Python Is Easy Learning python is easy
>>> Word[:4] , Word[4:] print(5 *"@") between
>>> Word[1:6:2] , print('3'+'7') 1.upper() and isalpha()  isalnum() 
Word[2:6:2] print('Hello'+ "Student") isupper() The Python isalpha() method returns isalnum() only returns true if a
>>> Word[-7:-3,3] , Word[::- 3. 2.title() and true if a string only contains letters. string contains alphanumeric
2] print("abc" =='abc') capitalize() characters(alphabets or numbers),
>>> Word[::-1] , Word[4:6] print('ABC'=='abc') 3.isalpha() and without symbols.
>>> Word[-3:-1] , Word[-4:-3] print('ABC'!="abc") isalnum() EG: EG:
>>>ord(‘a’),ord(‘z’) 4.strip() and split() str1='abcd' str1='abcd'
>>>ord(‘A’),ord(‘Z’) print(str1.isalpha()) print(str1.isalnum())
>>> chr(65),chr(90) str1 = 'learning pYTHon iS easy' str1 = 'learning pYTHon iS easy'
print(str1.isalpha() ) print(str1.isalnum() )
Upper() Isupper() str1='12abcd' str1='12abcd'
Python upper() method returns Python isupper() method returns print ( str1.isalpha()) print ( str1.isalnum())
str1=' ' str1='12abcd#'
print(str1.isalpha()) print ( str1.isalnum()) 4.
str1='12abcd#' str1=' '
print ( str1.isalpha()) print(str1.isalnum()))
Output Output
True True
False False
False True
False False
False False
Strip()  split()
It returns the string after removing split() method in Python split a 3.
the space from the both sides of string into a list of strings after upyHho#4yo
mahender,
the string. It Returns a copy of breaking the given string by the
SINGH,
the string specified separator.
dhoni
EG: EG:
string = " python is easy " text = 'Python is easy'
print(string.strip()) print(text .split())
print(string.strip(' saey ')) word = 'Python: is: easy'
print(word .split(':')) 5.
word = 'CatBatSatFatOr'
NA###5CV###4
print(word .split('t'))
4.
Output Output
##41
python is easy ['Python', 'is', 'easy']
python i ['Python', ' is', ' easy']
['Ca', 'Ba', 'Sa', 'Fa', 'Or']

Q14.Find the output:

1.
2. M. S. Dhoni
3 letters found

You might also like