Python Program to Create a Class in which One Method Accepts a String from the User and Another Prints it

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Object-Oriented Programming(OOPS):

Object-oriented programming (OOP) is a form of program structure that involves grouping related characteristics and activities into separate objects.

Objects are conceptually similar to system components. Consider a program to be a sort of factory assembly line. A system component processes some material at each step of the assembly line, eventually changing raw material into a finished product.

An object comprises data, such as the raw or preprocessed materials at each step of an assembly line, as well as behavior, such as the action performed by each assembly line component.

Python, like other general-purpose programming languages, has always been an object-oriented language. It enables us to create a program with an Object-Oriented approach. Classes and objects are simple to build and utilize in Python.

Using classes and objects, an object-oriented paradigm is used to construct the code. The object is associated with real-world entities such as a book, a house, a pencil, and so on. The oops notion is concerned with building reusable code. It is a common strategy for solving problems by constructing objects.

The task is to write a Python program that creates a class with one method that accepts a string from the user and another that prints it.

Examples:

Example1:

Input:

Enter some random string = btechgeeks

Output:

The string = btechgeeks

Example2:

Input:

Enter some random string = samokestring

Output:

The string = samokestring

Program to Create a Class in which One Method Accepts a String from the User and other Prints it in Python

Below is the full approach for writing a Python program that creates a class with one method that accepts a string from the user and another that prints it.

Approach:

  • Create a class and use the constructor to initialize the class’s values(here it is a string).
  • Create two methods inside the class.
  • getString: which scans the value of the given string.
  • putString: Which prints the value of the given string.
  • Create an object to represent the class.
  • Call both methods with the object created above.
  • Hence the string is scanned and printed.
  • The Exit of the Program.

Below is the implementation:

# creating a class
class stringClass():
  # use the constructor to initialize the class's values(here it is a string).
    def __init__(self):
        self.string = ""
    # Creating two methods inside the class.
    # getString: which scans the value of the given string

    def getString(self):
      # using input() function to scan the value of the string.
        self.string = input("Enter some random string = ")
   # putString: Which prints the value of the given string.

    def putString(self):
      # printing the string
        print("The string  =", self.string)


# Create an object to represent the class.
stringobj = stringClass()
# Call both methods with the object created above.
# calling getString method which scans the value of string
stringobj.getString()
# calling putString method which prints the value of the string
stringobj.putString()

Output:

Enter some random string = btechgeeks
The string = btechgeeks

Explanation:

  • A class called stringClass is created, and the __init__() function is used to set the string’s value to ” “(which is the default constructor in this case).
  • The first method (getString), Scan’s the string’s value from the user using input() function)
  • The second method (putString) is used to print the string’s value.
  • A class object called stringobj is created.
  • The methods getString() and putString() are printed using the object.
  • The string’s value which is scanned using getString is printed using putString.

Related Programs: