Practical Program Exercise 5 Display a string elements using class for 12th Computer Science
Display a string elements using class
Class
- Class is the main building block in Python.
- Object is a collection of data and function that act on those data.
- Class is a template for the object.
- According to the concept of Object Oriented Programming, objects are also called as instances of a class or class variable.
Defining classes
- In Python, a class is defined by using the keyword class.
- Every class has a unique name followed by a colon ( : ).
- Variables defined inside a class are called as “Class Variable” and functions are called as “Methods”.
- Class variable and methods are together known as members of the class.
- The class members should be accessed through objects or instance of class.
Syntax:
class class_name:
statement_1
statement_2
…………..
…………..
statement_n
Creating Objects
- Once a class is created, next you should create an object or instance of that class.
- The process of creating object is called as “Class Instantiation”.
Class Methods
- Python class function or Method is very similar to ordinary function with a small difference that, the class method must have the first argument named as self.
- No need to pass a value for this argument when we call the method
Constructor
- Constructor is the special function that is automatically executed when an object of a class is created.
- In Python, there is a special function called “init” which act as a Constructor.
- It must begin and end with double underscore.
- This function will act as an ordinary function; but only difference is, it is executed automatically when the object is created.
- This constructor function can be defined with or without arguments. This method is used to initialize the class variables.
Syntax
General format of __init__ method (Constructor function)
def __init__(self, [args ……..]):
<statements>
String
- String is a data type in python, which is used to handle array of characters.
- String is a sequence of Unicode characters that may be a combination of letters, numbers, or special symbols enclosed within single, double or even triple quotes.
PY 5 – DISPLAY A STRING ELEMENTS – USING CLASS
QUESTION:
- Write a program to accept a string and print the number of uppercase, lowercase, vowels, consonants and spaces in the given string using Class.
AIM:
- To accept a string and print the number of uppercase, lowercase, vowels, consonants and spaces in the given string using Class.
CODING:
class string:
def __init__(self):
self.uppercase=0
self.lowercase=0
self.vowels=0
self.consonants=0
self.spaces=0
self.string=””
def getstr(self):
self.string=str(input(“Enter the string:”))
def count_upper(self):
for ch in self.string:
if(ch.isupper()):
self.uppercase+=1
def count_lower(self):
for ch in self.string:
if(ch.islower()):
self.lowercase+=1
def count_vowels(self):
for ch in self.string:
if(ch in(‘A’,’a’,’E’,’e’,’i’,’I’,’o’,’O’,’u’,’U’)):
self.vowels+=1
def count_consonants(self):
for ch in self.string:
if(ch not in(‘A’,’a’,’E’,’e’,’i’,’I’,’o’,’O’,’u’,’U’,’ ‘)):
self.consonants+=1
def count_spaces(self):
for ch in self.string:
if(ch==” “):
self.spaces+=1
def display(self):
print(“The given string contains…..”)
print(“%d uppercase letters”%self.uppercase)
print(“%d lowercase letters”%self.lowercase)
def execute(self):
self.count_upper()
self.count_lower()
self.count_vowels()
self.count_consonants()
self.count_spaces()
s=string()
s.getstr()
s.execute()
s.display()
OUTPUT:
Enter the string: Welcome To Learn Computer Science
The given string contains…..
5 uppercase letters
24 lowercase letters
12 vowels
17 consonants
4 spaces