CS Knowledge Opener - Logo
CS Knowledge
Opener
Facebook Youtube Telegram
  • Home
  • 12th CS
    • 12th CS English Medium
    • 12th CS Tamil Medium
  • 12th CA
    • 12th CA English Medium
    • 12th CA Tamil Medium
  • 11th CS
    • 11th CS English Medium
    • 11th CS Tamil Medium
  • 11th CA
    • 11th CA English Medium
    • 11th CA Tamil Medium
  • CS / CA Question Bank
  • Other Subjects
  • Student Alerts
  • Govt Exams
  • Home
  • 12th CS
    • 12th CS English Medium
    • 12th CS Tamil Medium
  • 12th CA
    • 12th CA English Medium
    • 12th CA Tamil Medium
  • 11th CS
    • 11th CS English Medium
    • 11th CS Tamil Medium
  • 11th CA
    • 11th CA English Medium
    • 11th CA Tamil Medium
  • CS / CA Question Bank
  • Other Subjects
  • Student Alerts
  • Govt Exams
CS Knowledge Opener - Logo
CS Knowledge
Opener
Facebook Youtube Telegram
  • Home
  • 12th CS
    • 12th CS English Medium
    • 12th CS Tamil Medium
  • 12th CA
    • 12th CA English Medium
    • 12th CA Tamil Medium
  • 11th CS
    • 11th CS English Medium
    • 11th CS Tamil Medium
  • 11th CA
    • 11th CA English Medium
    • 11th CA Tamil Medium
  • CS / CA Question Bank
  • Other Subjects
  • Student Alerts
  • Govt Exams
  • Home
  • 12th CS
    • 12th CS English Medium
    • 12th CS Tamil Medium
  • 12th CA
    • 12th CA English Medium
    • 12th CA Tamil Medium
  • 11th CS
    • 11th CS English Medium
    • 11th CS Tamil Medium
  • 11th CA
    • 11th CA English Medium
    • 11th CA Tamil Medium
  • CS / CA Question Bank
  • Other Subjects
  • Student Alerts
  • Govt Exams
12th CS English Medium

Practical Program Exercise 5 Display a string elements using class for 12th Computer Science

Baskaran J
March 1, 2023 2 Mins Read
2.3K Views
0 Comments

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

 

 

 

Tags:

Practical

Share Article

Follow Me Written By

Baskaran J

Other Articles

Previous

Practical Program Exercise 4 Generate prime numbers and set operations for 12th Computer Science

Next

Public exam important top five mark questions for 12th computer science 2023

Next
March 6, 2023

Public exam important top five mark questions for 12th computer science 2023

Previous
March 1, 2023

Practical Program Exercise 4 Generate prime numbers and set operations for 12th Computer Science

No Comment! Be the first one.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

13 + 10 =

Related Posts

12th CS Board Exam Answer Key Discussion 2024-2025

Baskaran J
March 14, 2025

12th Computer Science English medium Program Question 2024-2025

Baskaran J
February 23, 2025

12th Computer Science Practice One marks question 2024-2025

Baskaran J
February 3, 2025

12th Computer Science English medium Public Exam Important Important Question 2024-2025

Baskaran J
January 16, 2025

Subscribe to our newsletter and stay updated.

CS Knowledge Opener

CS Knowledge Opener focuses on 11th &12th Computer Science & Computer Applications English Medium & Tamil Medium. Programming Skills and also we are Publishing Study Materials Based on New syllabus introduced by TNSCERT. (TNBOARD).

Quick Links

  • Contact Us
  • About Us
  • Privacy Policy
  • Disclaimer
  • Terms and Conditions
Categories
11th CA English Medium 15
11th CA Tamil Medium 9
11th CS English Medium 21
11th CS Tamil Medium 12
12th CA English Medium 32
12th CA Tamil Medium 8
12th CS English Medium 36
12th CS Tamil Medium 12
Government Exams 36
Notifications 8
Other Subjects 13

Follow Us

Youtube
Facebook
Telegram
CS Knowledge Opener © 2020 - 2025. All Rights Reserved.