{"id":10082,"date":"2021-09-30T12:00:11","date_gmt":"2021-09-30T06:30:11","guid":{"rendered":"https:\/\/python-programs.com\/?p=10082"},"modified":"2021-11-22T18:35:27","modified_gmt":"2021-11-22T13:05:27","slug":"python-program-to-create-a-class-in-which-one-method-accepts-a-string-from-the-user-and-another-prints-it","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-create-a-class-in-which-one-method-accepts-a-string-from-the-user-and-another-prints-it\/","title":{"rendered":"Python Program to Create a Class in which One Method Accepts a String from the User and Another Prints it"},"content":{"rendered":"
Beginners and experienced programmers can rely on these Best Java Programs Examples<\/a> and code various basic and complex logics in the Java programming language with ease.<\/p>\n Object-Oriented Programming(OOPS):<\/strong><\/p>\n Object-oriented programming (OOP) is a form of program structure that involves grouping related characteristics and activities into separate objects.<\/p>\n 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.<\/p>\n 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.<\/p>\n 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.<\/p>\n 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.<\/p>\n 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.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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 …<\/p>\nEnter some random string = btechgeeks<\/pre>\n
The string = btechgeeks<\/pre>\n
Enter some random string = samokestring<\/pre>\n
The string = samokestring<\/pre>\n
Program to Create a Class in which One Method Accepts a String from the User and other Prints it in Python<\/h2>\n
\n
# creating a class\r\nclass stringClass():\r\n # use the constructor to initialize the class's values(here it is a string).\r\n def __init__(self):\r\n self.string = \"\"\r\n # Creating two methods inside the class.\r\n # getString: which scans the value of the given string\r\n\r\n def getString(self):\r\n # using input() function to scan the value of the string.\r\n self.string = input(\"Enter some random string = \")\r\n # putString: Which prints the value of the given string.\r\n\r\n def putString(self):\r\n # printing the string\r\n print(\"The string =\", self.string)\r\n\r\n\r\n# Create an object to represent the class.\r\nstringobj = stringClass()\r\n# Call both methods with the object created above.\r\n# calling getString method which scans the value of string\r\nstringobj.getString()\r\n# calling putString method which prints the value of the string\r\nstringobj.putString()\r\n<\/pre>\n
Enter some random string = btechgeeks\r\nThe string = btechgeeks\r\n\r\n<\/pre>\n
\n
\n