This is a Python Program to accept and print a string using classes.
The program takes the string from the user and prints the string using classes.
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
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()
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.
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.
- Check Python Books
- Apply for Python Internship
- Practice Programming MCQs
- Check Information Technology Books
- Apply for Programming Internship