TORONTO KIDS COMPUTER CLUB | Aurora Saturday 14:30 Python Homework 11.23.
16760
post-template-default,single,single-post,postid-16760,single-format-standard,ajax_fade,page_not_loaded,,qode-theme-ver-7.6.2,wpb-js-composer js-comp-ver-6.8.0,vc_responsive

Aurora Saturday 14:30 Python Homework 11.23.

27 Nov Aurora Saturday 14:30 Python Homework 11.23.

Question:


Create an object WordReverse to reverse a string.

  • You need one argument (which is the original string) to create the object.
    • def __init__(self, word):
  • You need one method to reverse the string
    • def reverse(self):
  • You can print the object with the following expected output if the sample String is “1234abcd”
    • “1234abcd” => “dcba4321”

Hint1:

using len() to find out the length of the string e.g.

a = “Hello”
b = len(a) #b is now equal to 5

Hint2:

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”

Hint3:

We can use + or += to add string together

Hint4:

You can write the main program like following:

myString  = input(“Which string do you want to reverse? “)
wr = WordReverse(myString)
print(wr)

No Comments

Sorry, the comment form is closed at this time.