Given a string below:
str01 = 'This is a sample string'
Check if string am exists in string str01, print am is exist if it’s exist.
【Hint】
- Step 1: Do
if...elsecheck if stringamexists in the stringstr01- Step 2: Print
am is existif it does.
Write a program to convert the following list to string.
# Input
['This', 'is', 'a', 'sample', 'string']
# Output
"This is a sample string"
【Hint】
- Step 1: Define a function with a parameter lst.
python def convertor(lst):- Step 2: Define a empty string variable
stringto store the result string.- Step 3: Loop through the
lst, add each item to thestringlist with a space following behind.- Step 4: If it comes to the last item, skip adding the space.
- Step 5: Return the result
Reverse the given string:
# Input
'This is a sample string'
# Output
'gnirts elpmas a si sihT'
You’re encouraged to accomplish this exercise with algorithms (with
for, whileloops andif...elseconditionals). If it’s out of your ability, you’re allowed to usePythonbuild-in functions such asreverse()【Hint】
- Step 1: Define a function with a parameter
python def rev(string):- Step 2: Iterate through the string by
forloop.- Step 3: Define a empty string variable
result- Step 4: Add iterated items to the
resultvariable from the end to the beginning. Please pay close attention to the usage ofrange.