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...else
check if stringam
exists in the stringstr01
- Step 2: Print
am is exist
if 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
string
to store the result string.- Step 3: Loop through the
lst
, add each item to thestring
list 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, while
loops andif...else
conditionals). If it’s out of your ability, you’re allowed to usePython
build-in functions such asreverse()
【Hint】
- Step 1: Define a function with a parameter
python def rev(string):
- Step 2: Iterate through the string by
for
loop.- Step 3: Define a empty string variable
result
- Step 4: Add iterated items to the
result
variable from the end to the beginning. Please pay close attention to the usage ofrange
.