Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
92 views

String - Exercise

The document describes string operations performed on two sample strings - "Computer Science" and "WZ-1,New Ganga Nagar,New Delhi". It lists 10 string operations for each string and provides the output of each operation. Some key operations include printing substrings, finding/replacing substrings, converting case, splitting on delimiter, and checking startswith.

Uploaded by

Riya Tiwari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views

String - Exercise

The document describes string operations performed on two sample strings - "Computer Science" and "WZ-1,New Ganga Nagar,New Delhi". It lists 10 string operations for each string and provides the output of each operation. Some key operations include printing substrings, finding/replacing substrings, converting case, splitting on delimiter, and checking startswith.

Uploaded by

Riya Tiwari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Consider the following string mySubject:

mySubject = "Computer Science"


What will be the output of the following string operations:-

i. print(mySubject[0:len(mySubject)])
ii. print(mySubject[-7:-1])
iii. print(mySubject[::2])
iv. print(mySubject[len(mySubject)-1])
v. print(2*mySubject)
vi. print(mySubject[::-2])
vii. print(mySubject[:3] + mySubject[3:])
viii. print(mySubject.swapcase())
ix. print(mySubject.startswith('Comp'))
x. print(mySubject.isalpha())

ANSWER:

The output of the given string operations are as follows:

i. Computer Science
ii. Scienc
iii. Cmue cec
iv. e
v. Computer ScienceComputer Science
vi. eniSrtpo
vii. Computer Science
viii. cOMPUTER sCIENCE
ix. True
x. False

Question 2:

Consider the following string myAddress:


myAddress = "WZ-1,New Ganga Nagar,New Delhi"
What will be the output of following string operations :

i. print(myAddress.lower())
ii. print(myAddress.upper())
iii. print(myAddress.count('New'))
iv. print(myAddress.find('New'))
v. print(myAddress.rfind('New'))
vi. print(myAddress.split(','))
vii. print(myAddress.split(' '))
viii. print(myAddress.replace('New','Old'))
ix. print(myAddress.partition(','))
x. print(myAddress.index('Agra'))

ANSWER:

The output of the given string operations are as follows:

i. wz-1,new ganga nagar,new delhi


ii. WZ-1,NEW GANGA NAGAR,NEW DELHI
iii. 2
iv. 5
v. 21
vi. ['WZ-1', 'New Ganga Nagar', 'New Delhi']
vii. ['WZ-1,New', 'Ganga', 'Nagar,New', 'Delhi']
viii. WZ-1,Old Ganga Nagar,Old Delhi
ix. ('WZ-1', ',', 'New Ganga Nagar,New Delhi')
x. Error .. Substring Not found
Q1: WAP to display each character in same line.
Sol:
st= input(“Enter String”)
for ch in st:
print(ch, end=’ ‘)

Q2: WAP to count character ‘e’ in the given string.


Sol:
st= input(“Enter String”)
c=0
for ch in st:
if(ch==’e’):
c=c+1
print(“Occurrence of Character e is - ”,c)

Q3: WAP to replace character ‘l’ with character ‘*’ in the given string.
Ex:
Input: Hello World!
Output: He**o Wor*d
Sol:
st= input(“Enter String”)
for ch in st:
if(ch==’e’):
ch=’*’
print(ch , end=’ ‘)

Q4: WAP to replace vowels with character ‘*’ in the given string.
Ex:
Input: Hello World!
Output: H*ll* W*rld
Sol:
st= input(“Enter String”)
for ch in st:
if(ch in ’AEIOUaeiou’):
ch=’*’
print(ch , end=’ ‘)
Q5: WAP to count total number of words present in the given string.
Input: Have a nice Day
Output: 4
Sol:
st= input(“Enter String”)
sp=0
for ch in st:
if(ch==’ ‘):
sp=sp+1
print(“Total number of words are:”(sp+1))

Q6: WAP to display string in reverse order.


Input: Have a nice Day
Output: yaD ecin a evaH
Sol:
st= input(“Enter String”)
length= len(st)-1
for ch in range(-1,length, -1):
print(st[ch], end=’ ‘)

You might also like