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

Home » python » 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

  1. A class called print1 is created and the init() method is used to initialize the value of the string to “”.
  2. The first method, get, takes the value of the string from the user.
  3. The second string, put, is used to print the value of the string.
  4. An object for the class called obj is created.
  5. Using the object, the methods get() and put() is printed.
  6. The value of the string is printed.