Download as pdf
Download as pdf
You are on page 1of 33
(SN Pov 1 in vange ( Sens)-1): 4 = tinpat C" Bote, the String a) n ‘ “F 7 zinpat Enter the String") Yo tend Neefilee fot i in range ( feslsd-1, Seti re ¥+ SCID print Cx) ax 0» er @ Tet sl Ye 14 “e" te"F re TC! @ Y- h4 SJ tre Tel Yad Ye “oa! prt 09) Ce ae e a € h e 1 berld=2T dbnts)-t 26 a fel ="2" 0 Act "ACT [Ale] T ed | 2 2 o a b cvenet T(t se pi mth 2, \ieotee St ray! | Errors and da 14 In Python, errors’and exceptions are handled efficiently. The okeeater pe oe bled used to handle the exceptions. The tinally block is used to ates tbe ifshslemnemniy checking the.candition of the exception. The present chapter discusses in detail about err, exceptions, 14.1 Motivation During the execution of a program, there may bé a possibility of perceiving errors The three types of errors, They are + Syntax error © Logie error * Exception Syntax errors are errots generated during the parsing: So, they are also called asf errors. In the program, if there is. any syntax error, then the program will not be execute Output D:\PythonPrograms=python all. py File *al.py’, line & a ‘SyntaxError: unexpected EOF white p The above example code has a syntax error. The print statement does not end with during the program parsing, this error ts generated and the Program is not executed. The second type of error 's called as loge error. Logic errors do not Interrupt the exec ? 'y are: responsible for getting unwanted result, Fixing logic errors 14.4 Try Keyword The syr ax of the exception handling mechanism in Python contains three blocks. They are jy except and else blacks x=intinputt Enter the number1:")) y=int{input(“Enter the number2:")) ——_ _. print(The vaiue of number1:",x) Output Print(“The value of number2:*,y) 0:\PythonPrograms>python al.py Enter the number1:15 Enter the number2:0 The value of number: 15. The value of number2: 0 Number2 value is zero 14.7 With and Except Statements with ython will reduce th n = asks of clean ups and requir fw e. In this case, there is no need | with sutomatically closes the opene he exe 1s raised within the with block can a and ase should be aced inside a try block and the corresponding 45 Finally Keyword metal block can also be included w ithin the try block. If an excey ick then except block will be executed; otherwise. yk fet or alse blocks will be executed for the cen ected whether any condition of exce tekcode will be executed Ption is raised within the try else block will be executed. So, either Sponding try block. The finally block will be Ption fs raised or not. In both of these cases, the finally Output D\PythonPrograms>python al.py Enter the number 1:25 Enter the number2:5 The value of number: 25 The value of number2: 5 The result= 5.0 This is finally blook D:\PythonPrograms>python al.py Enter the number1:25, Enter the number2: Exception is raised This is finally block D:\PythonPrograms>python al.py Enter the number 1:25 Enter the number2:0 The value of number1: 25 The value of number2: 0 Exception is raised This is finally block ll =< Errors and Exceptions |27 raising Exceptions jous sections discussed about h. f AMAR exe ye io srogramming, the exeg, "B EXCeDtiOny that are Ptions can alse ber. | raised by default Howeve iciently in Python. The jed effic ) ython. The Syntax fog Ca ‘Ab seet Manually This ty pe af exception AR an “*Ception manually in given bolo Paine typeof 7 W#-mitesson( obit. trueabacht al exceptions are alsey handled bp it fatanil ; as of a string or class, 7A ER le owt of thee: rated ieeceptions « ¥ intyinputEnter the value of ay feintinputenter the value of by Output wini(*The value of a="*.a) DAPVthon ro i _ Programe>oython al py apomit Ma. veltie-of b=") calender bee 15 “ie tor the vaiue of ty 20 Me Romstion( wae siiiices The fois sxcopt Exception as e: The value of b= 26 “ipont"Received exception: “.e) Received axcaption value of a-fin.<0 49 Re-raising Exception Python programming, the last, exception can be re-raised The re raising of an exception seformed with the raise statement. Por this, multiple try-exoapt blocks are used TT Lee ae ae nd else block will be execu Enter the number2:5 The result= 5.0 This is finally block Enter the number2: Exception is raised This is finally block Exception is raised This is finally block Enter the number1:25 The value of number1: 25 The value of number2: 5 D:\PythonPrograms>python al.py Enter the number1:25 Enter the number2:0 The value of number: 25 The value of number2: 0 — os ited. So, either in an input string: Output O.\PythonPrograms>python b2 py Enter the input string: good The count of vowals: 2 Output D:\PythanPrograms>python b2 py Enter the input string: morning The count of consonants: 5 Output D:\PythonPrograms>python b2.p: Enter the input string alb20345 ‘The count of digit characters: 5 D:\PythonPrograms=python b2.py Enter the first string: if input string is a identifier * Output: D:\PythonPrograms>python b2 py Enter the string: abe The ascii code of ais 97 The ascii code of b is 98 The ascii code of cis 99 Output D:\PythonPrograms>python b2.py Enter the ascii code: 97 The character for the given asoii code of 87 isa @ ste WUusy) method refurns the ler ee use fillchar’) method to fill the empty positions ¢ justification. during Output D:\PythonPrograms=python a, s=input(“Enter the input string) Enter the input string: good ! t=int{input(“Enter the width to be justified:")) Enter the width to be justifies: y u=input(Enter the character to be filled:") Enter the character to be filled.» j aes.ijust(t.u) The loft justified string= gooey print(“The left justified string=“.a) The rjust() method returns the right justified input string within the specified width. It can ay, use fillchar() method to fill the empty positions during justification. Output D:\PythonPrograms>python b2 py | ssinput("Enter the input string:") Enter the input string:good t=int(input("Enter the width to be justified:")) Enter the width to be justified:7 u=input(“Enter the character to be filled:*) Enfan the chi 7 inter the cl aracter to be filled: steels The right justified string= ###go03 ) print(*The right justified string= *,a) The zfill method extends the width of the input string to the specified size by placing *0" in the empty positions. Output | \put("Enter ing”) D:\PythonPrograms>python b2>y | s=inj ‘the input string:” t=int(input("Enter tha wicth a enter : Enter the input string: good azs.zfil(t) Enter the width to be filled:7 The zero filled string= 000good print(“The zero filled string= *,a) Strings and Regular Expressions e37. p method! takes a string and returns the equivalent string with all lowercase Output Enter the input The lower case atrirg= fakes @ string and returns the equivalent string with all uppercas® D:\PythonProgramszpythan b2 py Enter the input string: good ‘Tha upper case string= GOOD input string by removing the specified character in the left for removal is whitespace. Output D:\PythonPrograms>python 62 oy Enter the input string: tttrttttthis is stripped: t the result of Istrip= his s good istified Input string within the spec Wied’ widthy, [t tay justification aN) The just method returns the left ji Use fillchar) method to fill the empty positions hiring output D:\PythonPrograms=python ba ‘Ssinput("Enter the inpul string) Enter tha input string: good teint{input(“Enter the width to be justified") Enter the width to be justifies ‘w=input("Enter the character to be filled") Enter the character 10 bs filled. The left justified string= goodayy print("The left justified string=*.a) The just method returns the right justified input string within the specified width. It cay a use fillohar) method to fill the empty positions during justification. Output = D:\\PythonPrograms=python 62, ere a nae conat tmidaitctrneges y ‘teintiinput(Enter the width to | eee ee) Enter the width to be justitied:7 ‘ucinput( Emer the character to be filed) Enter the character to be filled Drint("The sight|ustfied strings="a) ee eee ee The zfili) method extends the width of the input string to the specified size by placing “0” in the empty positions. Output D:\PythonPragrams>python b2.py Enter the input string: good Enter the width to be filled: The zero filled string= 000good j g, substring within the input string with the Output D:\PythonPrograms: Enter the input string: this ig...” Enter the existing substring .ig = The replace(method replaces the existiny substring, string") Enter the new substring: can be substring:") the result of strip= thcan be Canby ing") good it of strip=*,s.replace(t.z)) = The title) method converts the first character of each word in the input string to the Upper case character. Output D:\PythonPrograms>python b2 py Enter the input string:this is goog the result= This Is Good = The split) method splits the input string into various substrings. The splitting can be performed based on the number of required lines or based on the delimiter for splitting the string. Output D:\PythonPrograms>python b2.py Enter the input string:this is good Enter the delimiter:is the results ['th’,'', 19: Write a P’ 1 'ython program to check whether the input strings are anagrams of each other or not. Output D:\PythonPrograms>python b2.py Enter the first string:abo Enter the second string‘bea The two strings are anagrams A oo Bu > Ld i wie at jornet = roa Cory oo rn anil peewee ral 4 VR eyy + y Ed 2 . Pa a ocean et coer ten ee Cla at at M ee Poti et) cote Raia eater nae eres ees is a a * oc cree = oe rr en rey Process Tintshes with exit coge @ Sana | ae ee ee eR nT ee eee esha aero one? ease es re cc co ar nae Cee en Creer) Court) eT er MR a ae a ee oo 7” er cr Co ra jess finish ee renee errs Wet cr 1 Re cas ecuoyy Petree Cas Pty } Ct td ad ABERS cco pene Cd ‘wy Con) pt Oat rn Vo ead | His-chamical: jt eee | oe | ra —— amare aad a Perea ll ee Taal os a FI pT eC said = a oe Rit cctona rm Rao oom cr a eC een =. ie ote) pa

You might also like