In [25]:
class Stack:

    # Python list implementation of stacks

    def __init__(self):
    # construct an empty stack
        self._data = []

    def __str__(self):
    # string representation of internal state of the stack
        return "TOP: "+ str(self._data)
        
    def __len__(self):
    # returns number of items in stack
        return len(self._data)

    def is_empty(self):
    # returns True if stack is empty and False otherwise
        return self._data == []

    def top(self):
    # returns top item of stack it stack is not empty; otherwise returns None
        return self._data[0]

    def pop(self):
    # similar to top, except this also deletes top item from stack.
        x = self._data[0]
        self._data.remove(x)
        return x
        
    def push(self,e):
    # puts new item e on top of stack
        self._data = [e] + self._data
    
In [26]:
s1 = Stack()
In [27]:
s1.push(5)
In [28]:
print(s1)
TOP: [5]
In [29]:
s1.push(8)
In [30]:
print(s1)
TOP: [8, 5]
In [31]:
s1.push(3)
In [32]:
print(s1)
TOP: [3, 8, 5]
In [33]:
print(s1.pop())
3
In [34]:
print(s1)
TOP: [8, 5]
In [10]:
s = [1,2,3,4,5,6,7,8,9,10]
In [11]:
s[3]
Out[11]:
4
In [12]:
s[3:6]
Out[12]:
[4, 5, 6]
In [13]:
s[0]
Out[13]:
1
In [14]:
s.remove(s[0])
In [15]:
print(s)
[2, 3, 4, 5, 6, 7, 8, 9, 10]
In [18]:
len(s)
Out[18]:
9
In [20]:
s = "Sunderraman"
In [22]:
s.find('ram')
Out[22]:
6
In [23]:
s = [1,2,3]
In [24]:
str(s)
Out[24]:
'[1, 2, 3]'
In [35]:
def main():
    # implements the Reverse program
    while True:
        s = input("input a string (q to quit): ").strip()
        if s == 'q':
            break
        st = Stack()
        for c in s:
            st.push(c)
        t = ""
        while True:
            if st.is_empty():
                break
            t = t + st.pop()
        print("reverse of string: "+t)
            

main()
input a string (q to quit): abc
reverse of string: cba
input a string (q to quit): this is a long sentence
reverse of string: ecnetnes gnol a si siht
input a string (q to quit):    hello     
reverse of string: olleh
input a string (q to quit): q
In [ ]: