Lab3 String Functions
Lab3 String Functions
In [16]:
print("=================================================")
print(" PROGRAM 3A - INBUILD STRING FUNCTIONS ")
print("=================================================\n")
print("\n------------- Isalpha() and len() ---------------")
n=1
while n==1:## Name Validation
fname=input(" Enter First Name : ")
lname=input(" Enter Last Name : ")
#inbult function1 - isalpha(),checks for alphabet
if fname.strip().isalpha() and lname.strip().isalpha() :
break
else:
print ("Invalid Name, Please Enter Again")
n=1
print("\n-------------- IsDigit() and len() -------------")
while n==1:## Phone Number Validation
#inbult function2 - isdigit(),checks for digit
#inbult function3 - len() length of string
phone=input("\nCONTACT NUMBER : ")
if phone.strip().isdigit() and len(phone)==10:
break;
else:
print (" Invalid Phone Number, Please Enter Again")
n=1
while n==1:## ISBN Number Validation
=================================================
PROGRAM 3A - INBUILD STRING FUNCTIONS
=================================================
localhost:8888/notebooks/Documents/python/lab3_string_1841048.ipynb# 3/8
6/4/2020 lab3_string_1841048 - Jupyter Notebook
In [18]:
print("=================================================================\n")
print("PROGRAM 3B - STRING OPERATIONS (GIVEN PARAGRAPH)")
print("=================================================================\n")
mainstring="Python is an interpreted, high-level, general-purpose programming language.
print("------------------ Character With Hightest Frequency --------------\n")
#removing spaces to calculate frequency of characters
nospacestring=mainstring.replace(" ", "")
spl=",.-"
max2 = 0
for i in mainstring:
sc=mainstring.count(i)
if sc > max2:
l=i
max2 = mainstring.count(i)
print("{}[Space]---->{}".format(l,max2))
print("\n------------------ Characters With Second Hightest Frequency --------------\n")
max1 = 0
#check for second highest frequency
for i in nospacestring:
sc=nospacestring.count(i)
if sc > max1:
l=i
max1 = nospacestring.count(i)
print(" {} ---->{}".format(l,max1))
print("\n--------------------- Replacing Character With CapsLock -------------------\n")
#converting high frequency character to Capslock
caps=mainstring.replace(l,l.upper())
print(caps)
print("\n----------------------- Breaking The Paragraph Into Lines -----------------\n")
#using . as delimiter to spilt into sentences
linebreak=mainstring.split(".")
lc=len(linebreak)-1
for i in linebreak:
print(i.center(15))
#calculation of number of words
res1 = len(mainstring.split( ))
res2=len(mainstring.split("-"))
ress=(res1+res2)-1
print("\n------------------------------ Count Of Words ----------------------------\n")
print(" With Hyphen (-) {}".format(str(res1)))
print(" Without Hypehn(-) {}".format(str(ress)))
print("\n----------------------------- Count Of Lines ------------------------------\n")
#calculation of number of lines
print(" \nThe Number of Lines In The Given Paragraph is {} ".format(str(lc)))
#calculation of number of characters
charcheck=0
for char in mainstring:
charcheck=charcheck+1 #with space
print("\n--------------------------- Count Of Characters ---------------------------\n")
print(" \nThe Number of Characters (with white spaces) In The Given Paragraph is {} ".form
spacecheck=0
localhost:8888/notebooks/Documents/python/lab3_string_1841048.ipynb# 4/8
6/4/2020 lab3_string_1841048 - Jupyter Notebook
if(flag>-1):
mainstring1=mainstring
mainstring1=mainstring1.replace(ele1,"PyThOn")
mainstring1=mainstring1.replace(ele,"PyThOn")
mainstring1=mainstring1.replace(ele2,"PyThOn")
print(mainstring1)
else:
print("Pattern or Element not Found")
#Calculation of number of words excluding white spaces
print("\n------------------ Count Of Characters[ Excluding White Spaces ] --------------\n"
print("Count of Total Characters Excluding White Spaces are {} ".format(str(rcheck)))
print("====================================================================================
=================================================================
[Space]---->64
e ---->30
localhost:8888/notebooks/Documents/python/lab3_string_1841048.ipynb# 5/8
6/4/2020 lab3_string_1841048 - Jupyter Notebook
The Number of Characters (with white spaces) In The Given Paragraph is 381
localhost:8888/notebooks/Documents/python/lab3_string_1841048.ipynb# 6/8
6/4/2020 lab3_string_1841048 - Jupyter Notebook
In [19]:
#picks a random word and then "jumbles" it and player guesses the original word
import random
#list of words
WORDS = ("mango", "watermelon", "crow", "deer", "eagle", "apple", "goose", "hawk", "lion",
print("=================================================")
print(" PROGRAM 3C - JUMBLED WORDS ")
print("=================================================\n")
print('-------------------- WELCOME --------------------\n')
play='yes' #variable used to quit or continue the game
points=0 #to store the points
while play=="yes":
# picks one word randomly from the list
word = random.choice(WORDS)
# create a variable to use later to see if the guess is correct
correct = word
# create a jumbled version of the word
jumble =""
while word:
position = random.randrange(len(word))
jumble += word[position]
word = word[:position] + word[(position + 1):]
=================================================
PROGRAM 3C - JUMBLED WORDS
=================================================
-----------------------------------------
In [ ]:
localhost:8888/notebooks/Documents/python/lab3_string_1841048.ipynb# 8/8