0% found this document useful (0 votes)
28 views34 pages

Assignment 1700480105

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views34 pages

Assignment 1700480105

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

CHAPTER-10

STRINGS

UNIT - 2 (WEIGHTAGE: 45 Marks)


CHAPTER-10 SYLLABUS
Introduction,

String operations - concatenation, repetition, membership and slicing),

Traversing - Traversing a string using loops,

built-in functions –len()

22 string methods/functions:
isupper(), endswith() capitalize(), lstrip(), count(), replace() split(),
islower(), startswith() title(), rstrip(), find(), join() partition()
isalnum(), upper(), strip(), index(),
isalpha(), lower(),
isdigit(),
isspace(),
REVIEW OF CHAPTER-9

we learnt: 12 Questions

&

35 Lab Programs (i.e Lab programs-41 TO 75)


Question-1 2 MARKS
Explain the following 8 functions:
isupper(), islower(), isdigit(),
isalpha(), isalnum(), isspace(),
startswith(), endswith()
isupper() function
checks whether all the characters in a string are in
capital form and return either True or False.

Ex:
a=”cs lab”
print(a.isupper()) o/p: False

Syntax: string.isupper()
startswith() function
checks whether the string starts with a particulat substring(i.e
specified value) and returns either True or False

Ex:
a=”cs lab” o/p: False
print(a.startswith(“a”))

Syntax: string.startswith( substring)


Question-2 2 MARKS
Explain the following 7 functions:

capitalize(), title(),
upper(), lower(),
strip(), lstrip(), rstrip()
capitalize() function
● First, capitalize() function makes a copy of the given string
● Then, it converts the first letter of the copied string to capital.
● Finally, it returns the manipulated copy as result.

Ex:
a=”CS LAB”
print(a.capitalize()) o/p: Cs lab
print(a) CS LAB

Syntax: string.capitalize()
title() function
● First, title() function makes a copy of the given string
● Then, it converts the first letter of each word in the copied string to capital.
● Finally, it returns the manipulated copy as result.

Ex:
a=”CS LAB” o/p: Cs Lab
print(a.title()) CS LAB
print(a)

Syntax: string.title()
upper()
● First, upper() function makes a copy of the given string
● Then, it converts all the letters present in the copied string to capital.
● Finally, it returns that manipulated copy as result.

Ex:
a=”cs lab” o/p: CS LAB
print(a.upper()) cs lab
print(a)

Syntax: string.upper()
strip(), lstrip(), rstrip()
● First, strip() function makes a copy of the given string

● Then, it removes the specified character(s) present in the


beginning and at the end of the string

● FInally it returns that manipulated copy as result

Note: by default,
3 characters(space, tabspace and newline)
will be passed as input to strip() function
txt = ",,,,,rrttgg.....cricket....rrr"

x = txt.strip(",.grt")

print(x) o/p:
print(txt) cricke
,,,,,rrttgg.....cricket....rrr

working:
strip(“,.grt”) function checks whether any of these 5 characters are
present in the beginning and at the end of the copied string.

strip() function will be working like a loop.


The following 7 functions and the strip() function
to be recalled frequently
till we complete 12th standard
Question-3 2 MARKS
Explain the following 4 functions:

find(), index()
count(),
join()
find()
Ex:
a=”computer science lab”
o/p:
print(a.find(‘o’)) 1
print(a.find(‘e’)) 6

Syntax: string.find(substring, startindex=, stopindex=)

Description:
find() function checks whether the specified value(i.e substring) is present in the given string
or not.

If present, it returns the INDEX of its first occurrence.


If not, then it returns -1
index() same as find() function
Ex: o/p:
a=”computer science lab” 1
print(a.index(‘o’))
ERROR
print(a.index(‘e’))

Syntax: string.index(substring, startindex=, stopindex=)

Description:
index() function checks whether the specified value(i.e substring) is present in the given
string or not.

If present, it returns the INDEX of its first occurrence.


If not, then it returns -1
count() o/p:
2 1
example:
str= “the quick brown fox jumped over the river”
b=str.count(‘the’)
c=str.count('the', 11, 60)
print(b, c)

S1 = 'learn Python and Android” o/p:


1
S2 = S1.count(‘n’, 3, 11)
print(S2)
Syntax: string.count(substring, startindex=, stopindex=)
By default, startindex = 0 stopindex =len(string)

Description:
count() function returns the number of times the specified value
(i.e substring) appears in the given string.

If not present, then it returns 0


join() join() function combines the
elements of the sequence
Syntax: using separator string
string. join(“sequence”)

Note: sequence should be either


string data (or)
separator/joiner list of strings data (or)
tuple of strings data
example:
str= “Exam 2023”
o/p:
b=str.join(‘##’) #Exam 2023#
print(b) Exam 2023
print(str)

example:
str= “Exam 2023”
b=str.join(‘###’) o/p:
print(b) #Exam 2023#Exam 2023#
print(str) Exam 2023
Question-4 2 MARKS
Explain the following 3 functions:
replace(),
partition(),
split()
replace()
Syntax: string.replace(old substring, new substring)
Description
● First, replace() function makes a copy of the given string

● Then, it replaces the specified old substring with the new substring in the
copied string

● FInally it returns that manipulated copy as result

example:
str= 'dododo' o/p:
b=str.replace('do', 'c') ccc cococo dododo
c=str.replace('d', 'c')
print(b, c, str)
split() partition()

Description Description
split() function splits copy of the partition() function splits copy of the
given string into a list of words given string always into tuple of 3 words
using the specified separator. using the specified separator.
1st word - content before the separator
Note: by default,
2nd word - separator
space, tabspace and newline 3rd word - content after the separator
characters will be passed as input to
separator split() function

Syntax
string.split(separator=’ ‘, maxsplit=) Syntax
separator is optional parameter string.partition(separator)
maxsplit is optional parameter. (No of separator is compulsory parameter
splits is the input)
#EXAMPLE-1A: #EXAMPLE-2A: #EXAMPLE-3A:
txt = "welcome to the jungles" txt = "welcome to the jungles" s ="apple#grapes#cherry#orange"

x = txt.split('e') x = txt.split() res = s.split("#")

print(x) print(x) print(res)

#EXAMPLE-3B:
s ="apple#grapes#cherry#orange"
#EXAMPLE-1B: #EXAMPLE-2B:
txt = "welcome to the jungle" txt = "welcome to the jungle" res = s.partition("#")

x = txt.partition('e') x = txt.partition() print(res)

print(x) print(x) #EXAMPLE-3c:


s ="apple#grapes#cherry#orange"

res = s.split("#", 2)

1A O/P: ['w', 'lcom', ' to th', ' jungl', 's'] print(res)

1B O/P: ('w', 'e', 'lcome to the jungles')

2A O/P: ['welcome', 'to', 'the', 'jungles'] 3A O/P: ['apple', 'grapes', 'cherry', 'orange']

2B O/P: ERROR because no input is passed to separator 3B O/P: ('apple', '#', 'grapes#cherry#orange')

3C O/P: ['apple', 'grapes', 'cherry#orange']


STRING
PROGRAMS
PROGRAM-5a
Write a Python program to find the number of vowels present
in the given string.

program:(using indexing)
program: a=input(“Enter the string:”)
a=input(“Enter the string:”) c=0
c=0 for b in range(0, len(a)):
for b in a: if a[b] in “aeiou”:
if b in “aeiou”: c=c +1
c=c +1
print(“No of vowels=”, c)
print(“No of vowels=”, c)
.
PROGRAM-5b
Write a Python program to check whether the given string is
OUTPUT-1
palindrome or not. PALINDROME CHECKER
Enter any string: dad

Yes, the given string is palindrome

OUTPUT-2
PALINDROME CHECKER
program: Enter any string: python

a=input(“Enter the string:”) No, the given string is not palindrome

b=a[::-1]
if a==b:
print(“The given string is palindrome “)
else:
print(“The given string is not palindrome “)
OUTPUT
QUESTIONS
6a. Find and write the output of the following python code :

Msg1="WeLcOME"
Msg2="GUeSTs"
Msg3=""
for I in range(0,len(Msg1):
if Msg1[I]>="A" and Msg1[I]<="M":
Msg3=Msg3+Msg1[I]
elif Msg1[I]>="N" and Msg1[I]<="Z":
Msg3=Msg3+Msg2[I]
else:
Msg3=Msg3+"*"

print(Msg3) o/p: G*L*TME


6b. Find and write the output of the following python code :

s='School2@com'
k=len(s)
m=" "
for i in range(0,k):
if(s[i].isupper()):
m=m+s[i].lower()
elif s[i].isalpha():
m=m+s[i].upper()
else:
m=m+'bb'
o/p:
print(m) sCHOOLbbbbCOM
6c Find and write the output of the following python code :
m=""
str='[email protected]'
for i in range(0,len(str)):
if(str[i].isupper()):
m=m+str[i].lower()
elif str[i].islower():
m=m+str[i].upper()
else:
if i%2==0:
m=m+str[i-1]
else:
m=m+"#" o/p:

fUN#pYTHONn#.
print(m)
Last two are
THEORY QUESTIONS
Question-7 2 MARKS
what is String Traversing?

processing each and every character in the given


string is referred as Traversing.

example:
inorder to find the number of vowels present in the
given string, each and every character has to be
processed. This activity is called as Traversing.
Question-8 2 MARKS
List out String operations.

4 string operations
● concatenation
● replication
● membership ex: if b in “aeiou”:
● slicing

we will learn string slicing along with list slicing in CHAPTER-11

You might also like