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

This is a Python Program to accept and print a string using classes.

Problem Description

The program takes the string from the user and prints the string using classes.

Problem Solution

1. Create a class and using a constructor initialize values of that class.
3. Create two methods called as get which takes in the value of a string and another called put that prints the string.
4. Create an object for the class.
5. Using the object, call both the methods.
6. The string is printed.
7. Exit

Program/Source Code

Here is the source code of the Python Program that takes the string from the user and prints the string using classes. The program output is also shown below.

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 “”.
3. 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.
5. An object for the class called obj is created.
6. Using the object, the methods get() and put() is printed.
7. The value of the string is printed.

advertisement
advertisement
Runtime Test Cases
 
Case 1:
Enter string: Hello world
String is:
Hello world
 
Case 2:
Enter string: Sanfoundry is a very helpful website
String is:
Sanfoundry is a very helpful website

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Note: Join free Sanfoundry classes at Telegram or Youtube

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.