w3resource

Python: A class has two methods. First method accept a string from the user, Second method print the string in upper case


9. Class with get_String and print_String Methods

Write a Python class that has two methods: get_String and print_String , get_String accept a string from the user and print_String prints the string in upper case.

Sample Solution:

Python Code:

class IOString():
    def __init__(self):
        self.str1 = ""

    def get_String(self):
        self.str1 = input()

    def print_String(self):
        print(self.str1.upper())

str1 = IOString()
str1.get_String()
str1.print_String()

Sample Output:

w3resource                                                                                                    
W3RESOURCE 

Pictorial Presentation:

Python: A class has two methods. First method  accept a string from the user, Second method  print the string in upper case.

Flowchart:

Flowchart: A class has two methods. First method  accept a string from the user, Second method  print the string in upper case

For more Practice: Solve these Related Problems:

  • Write a Python class that accepts a string input from the user and then prints the string in uppercase using two separate methods.
  • Write a Python class that implements get_String() to read a line from input and print_String() to display it reversed in uppercase.
  • Write a Python class that stores a string as an instance variable and provides methods to output it in both lower and upper case.
  • Write a Python class that uses the property decorator to manage a string attribute and includes methods to print the string in modified forms.

Go to:


Previous: Write a Python class to reverse a string word by word.
Next: Write a Python class named Rectangle constructed by a length and width and a method which will compute the area of a rectangle.

Python Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.