09 Feb Markham Saturday 9:30 Python Homework 02.08.
Question:
Create an object WordReverse to reverse a string.
- You need one argument (which is the original string) to create the object, for example:
def __init__(self, word): - You need one method to reverse the string and it will return a reversed string, you can define the method as following:
def reverse(self): - You can print the object with the following expected output if the sample String is “1234abcd” (it will show the original string and reversed string, and => in between):
“1234abcd” => “dcba4321”
Hint 1:
using len() to find out the length of the string e.g.
a = 'Hello'
b = len(a) #b is now equal to 5
Hint 2:
String is also a kind of list, so we can using [] to access each character of the string e.g.
a = 'Hello'
b = a[1] #b now is letter ‘e’ in the “Hello”
Hint 3:
We can use + or += to add string together
Hint 4:
You can write the main program like following:
myString = input('Which string do you want to reverse? ')
wr = WordReverse(myString)
print(wr)
Sorry, the comment form is closed at this time.