String Manipulation Problem Statement
String Manipulation Problem Statement
Instructions:
Please share your answers filled inline in the word document. Submit code files wherever
applicable.
Name: _________________________
Problem Statement:
It is obvious that as part of data analysis we encounter a lot of text data which is a
collection of strings which in turn is a sequence of characters. Access the text data and
manipulate as per our requirements. you can go through this link for further assistance:
https://360digitmg.com/mindmap-data-science
1. Create a string “Grow Gratitude”.
Code for the following tasks:
a) How do you access the letter “G” of “Growth”?
b) How do you find the length of the string?
#length
word='Grow Gratitude'
len(word) =14
c) Count how many times “G” is in the string.
message = 'Grow Gratitude'
# number of occurrence of 'G'
print('Number of occurrence of G:', message.count('G'))
2
3. Create a string "Idealistic as it may sound, altruism should be the driving force in
business, not just competition and a desire for wealth"
Code for the following tasks:
d= ‘Idealistic as it may sound, altruism should be the driving force in business,
not just competition and a desire for wealth’
a) get one char of the word d[0] =’I’
b) get the first three char d[0:3] =’Ide’
c) get the last three char d[119:122]=’lth’
4. create a string "stay positive and optimistic". Now write a code to split on
whitespace.
Write a code to find if:
a) The string starts with “H”
x = txt.split('H')
print(x)= ['stay positive and optimistic']
b) The string ends with “d”
x= txt.split('d',1)
print(x) = ['stay positive an', ' optimistic']
c) The string ends with “c”
x= txt.split('c')
print(x)= ['stay positive and optimisti', '']
5. Write a code to print " 🪐 " one hundred and eight times. (only in python)
🪐*108
print ('🪐' * 108)
7.Create a string “Grow Gratitude” and write a code to replace “Grow” with “Growth
of”
string = "Grow Gratitude"
# replace all instances of 'Grow' (old) with 'Growth of' (new)
new_string = string.replace("Grow", "Growth of" )
print(string) = Grow Gratitude
print(new_string) =Growth of Gratitude
8.A story was printed in a pdf, which isn’t making any sense. i.e.:
“.elgnujehtotniffo deps mehtfohtoB .eerfnoilehttesotseporeht no dewangdnanar
eh ,ylkciuQ .elbuortninoilehtdecitondnatsapdeklawesuomeht ,nooS .repmihwotd
etratsdnatuotegotgnilggurts saw noilehT .eert a
tsniagapumihdeityehT .mehthtiwnoilehtkootdnatserofehtotniemacsretnuhwef
a ,yad enO .ogmihteldnaecnedifnocs’esuomeht ta dehgualnoilehT ”.emevasuoy fi
yademosuoyotplehtaergfo eb lliw I ,uoyesimorp
I“ .eerfmihtesotnoilehtdetseuqeryletarepsedesuomehtnehwesuomehttaeottuoba
saw eH .yrgnaetiuqpuekow eh dna ,peels
s’noilehtdebrutsidsihT .nufroftsujydobsihnwoddnapugninnurdetratsesuom a
nehwelgnujehtnignipeelsecno saw noil A”
def my_function(x):
return x[::-1]
print(mytxt)
A lion was oncesleepinginthejunglewhen a
mousestartedrunningupanddownhisbodyjustforfun. Thisdisturbedthelion’s sleep,
and he wokeupquiteangry. He was
abouttoeatthemousewhenthemousedesperatelyrequestedtheliontosethimfree. “I
promiseyou, I will be ofgreathelptoyousomeday if yousaveme.” Thelionlaughed
at themouse’sconfidenceandlethimgo. One day, a
fewhunterscameintotheforestandtookthelionwiththem. Theytiedhimupagainst a
tree. Thelion was strugglingtogetoutandstartedtowhimper. Soon,
themousewalkedpastandnoticedthelionintrouble. Quickly, he ranandgnawed on
theropestosetthelionfree. Bothofthem sped offintothejungle.
You have noticed that the story is printed in a reversed order. Rectify the same and
write a code to print the same story in a correct order.
Hints:
For each assignment, the solution should be submitted in the below format
1. Research and perform all possible steps for obtaining solution
3. All the codes (executable programs) should execute without errors
4. Code modularization should be followed
5. Each line of code should have comments explaining the logic and why you are using that
function