Uploaded by musab.nomani

ASSIGNMENT PF 2 EDITED 2.0 (1)

advertisement
ASSIGNMENT 2
SIR SYED UNIVRSITY OF ENGINEERING AND TECHNOLOGY
Program: DATA SCIENCE
Course instructor: Mr. Ali Yousuf
Students: Rumaisa Syed (069)
Hiba Rizwan (013)
Myra Arshad (036)
Programming Fundamentals
Question 1:
Compute and show demonstration, syntax, and example(s) of the following methods in Python
i.e.:
a) upper()
b) lower()
c) title()
d) print()
e) strip()
f) rstrip()
g) lstrip()
h) split()
i) input()
j) eval()
Answer 1:
Method
Syntax
Lower ()
string.lower()
Title()
string.title()
Description
Example
Code:
The lower() method
returns a string where all txt="Welcome Home"
characters are lower case. x=txt.lower()
print(x)
Symbols and Numbers
Output:
are ignored.
>>> %Run lower.py
welcome home
>>>
The title() method
returns a string where the
first character in every
word is upper case. Like a
header, or a title.
Code:
txt="i would like order
pizza"
x=txt.title()
print(x)
If the word contains a
number or a symbol, the
first letter after that will be
converted to upper case.
Output:
>>> %Run title.py
I Would Like Order Pizza
>>>
1
Programming Fundamentals
Print()
Strip()
print(object(s)
String.strip(characters)
The print() function
prints the specified
message to the screen, or
other standard output
device.
Code:
print("My name is XYZ")
The strip() method
removes any leading, and
trailing whitespaces.
Code:
txt=" blue sweater "
x=txt.strip()
print("Of all the sweaters",x,
"is his favourite")
Leading means at the
beginning of the string,
trailing means at the end.
You can specify which
character(s) to remove, if
not, any whitespaces will
be removed
Output:
>>> Run print.py
My name is XYZ
>>>
Output:
>>> %Run strip.py
Of all the sweaters blue
sweater is his favourite
>>>
Code:
txt="bghggh...,HELLO..,,BJU"
x=txt.strip("bgh.,BJU")
print(x)
Output:
>>> %Run strip.py
HELLO
>>>
Rstrip()
string.rstrip(characters) The rstrip() method
removes any trailing
characters (characters at
the end a string), space is
the default trailing
character to remove.
2
Code:
txt=(" hello again...,,")
x=txt.rstrip(".,")
print(x)
Output:
>>> %Run rstrip.py
hello again
>>>
Programming Fundamentals
Split()
string.split(separator,
maxsplit)
The split() method splits
a string into a list.
You can specify the
separator, default separator
is any whitespace.
Code:
txt=("I#would#like#to#order
#pizza,#burger#
and#honeyjam")
x=txt.split("#")
print(x)
Output:
>>> %Run split.py
['I', 'would', 'like', 'to', 'order',
'pizza,', 'burger', 'and',
'honeyjam']
>>>
Input()
input(prompt)
The input() function allows
user input.
Code:
print('Enter your name:')
x = input()
print('Hello, ' + x)
Output:
>>> %Run input.py
Enter your name:
ALEX
Hello, ALEX
>>>
Eval()
lstrip()
eval(expression, globals, The eval () function evaluates
locals)
the specified expression, if
the expression is a legal
Python statement, it will be
executed.
Code:
x='print(89)'
eval(x)
string.strip([characters]) The lstrip() method
removes any leading
characters (space is
the default leading
character to remove)
Code:
txt=(" England
x=txt.lstrip()
print(x)
Output:
>>> %Run eval.py
89
>>>
Output:
>>> Run lstrip.py
England
>>>
3
")
Programming Fundamentals
Answer:
Input:
phrase= input("Enter a string phrase = " )
#initializing counts
vowels=0
consonants=0
#assigning values to variables
total_vowels= "aeiouAEIOU"
total_consonants= "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ"
#calculating vowels and characters
for char in phrase:
if char in total_vowels:
vowels +=1
elif char in total_consonants:
consonants +=1
#calculating spaces total words and characters
spaces= (phrase.count(' '))
total_words=len(phrase.split())
4
Programming Fundamentals
total_char=len(phrase)-spaces
#printing the results
print("Total vowels =", vowels )
print("Total consonants =", consonants)
print("Total spaces =", spaces)
print("Total words =", total_words)
print("Total character (excluding spaces) =", total_char)
Output:
>>> %Run assig2.py
Enter a string phrase = You Are A Great Teacher
Total vowels = 10
Total consonants = 9
Total spaces = 4
Total words = 5
Total characters (excluding spaces) = 19
>>>
5
Download