6/4/2020 lab3_string_1841048 - Jupyter Notebook
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
isbn=input("\nEnter ISBN Number of the Book : ")
if isbn.strip().isdigit() and (len(isbn)==13 or len(isbn)==10):
break;
else:
print (" Invalid ISBN Number, Please Enter Again")
n=1
bk=input(" Enter Book Name : ")
at=input(" Enter Author : ")
##inbult function4- concatenation - joins two or more strings
print("\n ------------- String Concatenation --------------")
print(" \nFull Name : {}".format(fname+" "+lname))
##inbult function5- find
print(" \n--------------------- Find() --------------------")
ele=input("Enter the Search Element in Author's Name: ")
pos=at.find(ele)
if pos>-1:
print ("\nThe Element is found in the index position : {}".format(str(pos)))
else:
print("Element Not Found\n")
##inbult function6- count - frequency of given word
print(" \n------------------- Count() ---------------------")
n=input("Enter the seach element In ISBN Number : ")
ct=isbn.count(n)
if(ct>-1):
print("\nThe Count of Number {} in ISBN Number is {}".format(n,str(ct)))
else:
print("Number Not Found In the Input")
##inbult function7- title() - turns first letter of every word to capital
print("\n------------------- Title() -----------------------")
print("\nThe Title Of The Book is {}".format(bk.title()))
##inbult function8- Capitalize() - First Alphabet to capital
localhost:8888/notebooks/Documents/python/lab3_string_1841048.ipynb# 2/8
6/4/2020 lab3_string_1841048 - Jupyter Notebook
print(" \n---------------- Capitalize() --------------------")
print("\nThe Author Of The Book is {}".format(at.capitalize()))
print("========================================================\n")
=================================================
PROGRAM 3A - INBUILD STRING FUNCTIONS
=================================================
------------- Isalpha() and len() ---------------
Enter First Name : sarah
Enter Last Name : sonali
-------------- IsDigit() and len() -------------
CONTACT NUMBER : 1254kh
Invalid Phone Number, Please Enter Again
CONTACT NUMBER : 9741542896
Enter ISBN Number of the Book : 9783161484100
Enter Book Name : the fault in our stars
Enter Author : john green
------------- String Concatenation --------------
Full Name : sarah sonali
--------------------- Find() --------------------
Enter the Search Element in Author's Name: n
The Element is found in the index position : 3
------------------- Count() ---------------------
Enter the seach element In ISBN Number : 5
The Count of Number 5 in ISBN Number is 0
------------------- Title() -----------------------
The Title Of The Book is The Fault In Our Stars
---------------- Capitalize() --------------------
The Author Of The Book is John green
========================================================
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
for char in mainstring:
if(char==" "):
spacecheck=spacecheck+1 #without spaces
rcheck=charcheck-spacecheck
print(" \nThe Number of Characters (without white spaces) In The Given Paragraph is {} ".f
coma=linebreak[0]
print(coma)
print("\n--------------------- Split Of First Line [Coma Delimiter] ----------------\n")
#split of first sentence using coma as delimiter
comasplit=coma.split(",")
print("\n* * * List of Split Words * * *\n")
print(comasplit)
for i in comasplit:
print(i)
print("\n------------------ Find And Replace [Python, python] ----------------------\n")
#using find and replace - PyThOn
ele=input("Enter the String to be Replaced : ")
ele1=ele.capitalize()#first letter to capiital
ele2=ele.lower()#all letters to lower case
flag=mainstring.find(ele)#searching word
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("====================================================================================
=================================================================
PROGRAM 3B - STRING OPERATIONS (GIVEN PARAGRAPH)
=================================================================
------------------ Character With Hightest Frequency --------------
[Space]---->64
------------------ Characters With Second Hightest Frequency --------------
e ---->30
--------------------- Replacing Character With CapsLock -------------------
Python is an intErprEtEd, high-lEvEl, gEnEral-purposE programming languagE.
Guido van Rossum is thE fathEr of python. In thE yEar 1991 pytthon was
officially rElEasEd. Python's dEsign philosophy EmphasizEs codE rEadabili
ty with its notablE usE of significant whitEspacE. Python follows objEct-ori
EntEd approach. Python is famous as all purposE programming languagE.
----------------------- Breaking The Paragraph Into Lines -----------------
Python is an interpreted, high-level, general-purpose programming language
Guido van Rossum is the father of python
localhost:8888/notebooks/Documents/python/lab3_string_1841048.ipynb# 5/8
6/4/2020 lab3_string_1841048 - Jupyter Notebook
In the year 1991 pytthon was officially released
Python's design philosophy emphasizes code readability with its notable
use of significant whitespace
Python follows object-oriented approach
Python is famous as all purpose programming language
------------------------------ Count Of Words ----------------------------
With Hyphen (-) 49
Without Hypehn(-) 52
----------------------------- Count Of Lines ------------------------------
The Number of Lines In The Given Paragraph is 6
--------------------------- Count Of Characters ---------------------------
The Number of Characters (with white spaces) In The Given Paragraph is 381
The Number of Characters (without white spaces) In The Given Paragraph is 3
17
Python is an interpreted, high-level, general-purpose programming language
--------------------- Split Of First Line [Coma Delimiter] ----------------
* * * List of Split Words * * *
['Python is an interpreted', ' high-level', ' general-purpose programming la
nguage']
Python is an interpreted
high-level
general-purpose programming language
------------------ Find And Replace [Python, python] ----------------------
Enter the String to be Replaced : Python
PyThOn is an interpreted, high-level, general-purpose programming language.
Guido van Rossum is the father of PyThOn. In the year 1991 pytthon was
officially released. PyThOn's design philosophy emphasizes code readabili
ty with its notable use of significant whitespace. PyThOn follows object-ori
ented approach. PyThOn is famous as all purpose programming language.
------------------ Count Of Characters[ Excluding White Spaces ] -----------
---
Count of Total Characters Excluding White Spaces are 317
============================================================================
========
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):]
print("The jumbled word is: ", jumble)
while True:
guess=input("\nEnter your answer: ")
print("----------------------------------------- \n")
f2=guess.isalpha()
if f2 == False :
print('Invalid Input')
else :
break
#guess = input("\nEnter your answer: ")
if guess == correct:
points+=1 #point is increased by 1
print("* * * Congrats! Correct answer. * * *\n")
print("Your score is: "+str(points))
else :
points-=1 #point is decreased by 1
print("Wrong Answer.")
for i in WORDS:
if correct==i :
print('* * * The correct answer was : {} * * * '.format(i))
print("Your score is: "+str(points))
play=input("To continue the game enter 'yes'")
print('Your final Score is : ', points)
print("--------------------- THANK TOU ----------------------")
=================================================
PROGRAM 3C - JUMBLED WORDS
=================================================
-------------------- WELCOME --------------------
The jumbled word is: rowc
Enter your answer: crow
localhost:8888/notebooks/Documents/python/lab3_string_1841048.ipynb# 7/8
6/4/2020 lab3_string_1841048 - Jupyter Notebook
-----------------------------------------
* * * Congrats! Correct answer. * * *
Your score is: 1
To continue the game enter 'yes'yes
The jumbled word is: eerd
Enter your answer: deer
-----------------------------------------
* * * Congrats! Correct answer. * * *
Your score is: 2
To continue the game enter 'yes'yes
The jumbled word is: orcw
Enter your answer: crow
-----------------------------------------
* * * Congrats! Correct answer. * * *
Your score is: 3
To continue the game enter 'yes'yes
The jumbled word is: egale
Enter your answer: eagle
-----------------------------------------
* * * Congrats! Correct answer. * * *
Your score is: 4
To continue the game enter 'yes'no
Your final Score is : 4
--------------------- THANK TOU ----------------------
In [ ]:
localhost:8888/notebooks/Documents/python/lab3_string_1841048.ipynb# 8/8