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'))
Question
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'))
✨ Free AI Tools for You
Powered by Chatterbot AI
Answer
Here are the outputs for each of the string operations:
Converting the string to lowercase:
print(myAddress.lower()) Output: wz-1,new ganga nagar,new delhi
Converting the string to uppercase:
print(myAddress.upper()) Output: WZ-1,NEW GANGA NAGAR,NEW DELHI
Counting the number of times 'New' appears in the string:
print(myAddress.count('New')) Output: 2
Finding the first occurrence of 'New':
print(myAddress.find('New')) Output: 5
Finding the last occurrence of 'New':
print(myAddress.rfind('New')) Output: 20
Splitting the string by commas:
print(myAddress.split(',')) Output: ['WZ-1', 'New Ganga Nagar', 'New Delhi']
Splitting the string by spaces:
print(myAddress.split(' ')) Output: ['WZ-1,New', 'Ganga', 'Nagar,New', 'Delhi']
Replacing 'New' with 'Old':
print(myAddress.replace('New', 'Old')) Output: WZ-1,Old Ganga Nagar,Old Delhi
Partitioning the string at the first comma:
print(myAddress.partition(',')) Output: ('WZ-1', ',', 'New Ganga Nagar,New Delhi')
Finding the index of 'Agra':
print(myAddress.index('Agra')) Output: ValueError: substring not found
For the last operation (
print(myAddress.index('Agra'))
), it will throw aValueError
since 'Agra' does not exist in the stringmyAddress
.
India's 1st AI Doubt Solver for CBSE, JEE, and NEET
Ask a Question for Freeand then it's just ₹212 a month