by BehindJava
Write a program to Create a Class in which One Method Accepts a String from the User and Another Prints it in Python
In this tutorial we are going to learn about creating a Class in which One Method Accepts a String from the User and Another Prints it in Python.
Python Program to Create a Class in which One Method Accepts a String from the User and Another Prints it
class print1():
def __init__(self):
self.string=""
def get(self):
self.string=input("Enter string: ")
def put(self):
print("String is:")
print(self.string)
obj=print1()
obj.get()
obj.put()
Program Explanation
- A class called print1 is created and the init() method is used to initialize the value of the string to “”.
- The first method, get, takes the value of the string from the user.
- The second string, put, is used to print the value of the string.
- An object for the class called obj is created.
- Using the object, the methods get() and put() is printed.
- The value of the string is printed.