You are on page 1of 5

SIR SYED UNIVERSITY OF ENGINEERING AND TECHNOLOGY

SOFTWARE ENGINEERING DEPARTMENT


FALL 2021F
Programming Fundamentals (SWE-102)
Assignment # 2

Date : 21/12/2021

Question # 1:
Write down the description, syntax and example of the following methods in python.

Method Syntax Description Example


upper() string.upper() The upper() method Code:
returns a string where str=”hello python”
all characters are in print(str.upper())
upper case.
Output:
>>> %Run
asssignmen2.py
HELLO PYTHON

lower() string.lower() The lower method Code:


returns a string where str=”HELLO
all characters are in PYTHON”
lower case. print(str.upper())
Output:
>>> %Run
asssignmen2.py
hello python

title() string.title() The title method Code:


returns a string in str=”HELLO
which the first letter PYTHON”
in each word is print(str.title())
uppercase the rest are Output:
lowercase. >>> %Run
asssignmen2.py
Hello Python
print() print(“string”) The print() function Code:
takes in any number str=”Hello Python”
of parameters, and print(str)
prints them out on
one line of text.  Output:
>>> %Run
asssignmen2.py
Hello Python
strip() string.strip()) The strip() method Code:
returns a copy of the str=”Hello Python”
string with both print(str.strip(“Hello”))
leading and trailing
characters stripped. Output:
>>> %Run
asssignmen2.py
Python
rstrip() string.rstrip() The rstrip() method Code:
returns a copy of the str=”Hello Python”
string with trailing print(str.rstrip(“Python))
characters stripped.
Output:
>>> %Run
asssignmen2.py
Hello
lstrip() string.lstrip. The lstrip() method Code:
returns a copy of the str=”Hello Python”
string with leading print(str.lstrip(“Hello”))
characters stripped.
Output:
>>> %Run
asssignmen2.py
Python
split() string.split() The strip method Code:
returns a list of string str=”Hello Python”
after breaking the print(str.split())
given string by the
specified separator. Output:
>> %Run
asssignmen2.py
['Hello', 'Python']
input() string=input() The input method Code:
takes input from the str=input(“enter a
user then identifies string”)
whether the given print(str)
input is integer or Output:
string. >>> %Run
asssignmen2.py
enter a string: hello
python
hello python
eval() string=eval(input()) The eval() method Code:
parses the expression str=eval(input(“Enter a
passed to this method Value: “))
and runs python print(str)
expression (code) Output:
within the program. >>> %Run
asssignmen2.py
enter a string: 55
55

Question # 2:
Write a python program to take string phrase input from the user through
keyboard. The program should
be able to print the following.
(a) Total Vowels (upper or lower case character) in the given string phrase.
(b) Total Consonants (upper or lower case character) in the given string phrase.
(c) Total Spaces in the given string phrase.
(d) Total words in the given string phrase.
(e) Total characters in the given string phrase excluding spaces.

Code:
def strph(str):
vowels = 0
consonant = 0
totalwords = 0
totalchar = 0
totalspc = 0

for i in range(0, len(str)):


ch = str[i]
if ( (ch >= 'a' and ch <= 'z') or (ch >= 'A' and ch <= 'Z') ):
ch = ch.lower()

if (ch == 'a' or ch == 'e' or ch == 'i'


or ch == 'o' or ch == 'u'):
vowels += 1
else:
consonant += 1

totalwords=str.split()
totalwords=len(totalwords)

for i in range(len(str)):
if(str[i].isalpha()):
totalchar = totalchar + 1

for i in range(0, len(str)):

if str[i] == " ":


totalspc+= 1

print("Vowels:", vowels)
print("Consonant:", consonant)
print("Total Words:", totalwords)
print("Total Characters:", totalchar)
print("Total Space:", totalspc)

str=str(input("Enter A String Phrase: "))


strph(str)
Ouput:

You might also like