You are on page 1of 10

Date /20

wntle Code Snippe o a.ccept the Sentt ne hon he usta


diaplay the lOngus wond of hat Statnu cule with Pts
length
prindEntr the Stntnce)

Stndente : oput )

eg longesmax (Statnte Spftd D, kegele)


pn longus{ wotd fsJongasà
priat"And sLngth s J (Jongsst))

Fo gutn Gstnunbin 45,38,4, 65,93.1 ogtt a pyihon


PuoGnEm nep lace o he by 3 h
iotegensdivisibl

PPP Cnd Q fnttatns diyisibu b 5 h g 9 Oad


ntpl4ce a
Jnp[ace hioitQ4uns divisi hlu by boBh 3 ds_oth"PPpga
dsela he Oudpud

num u s , 2a,14,65, Q112


or ia non(Ln Cnum))}
(num % 5 0 )and (num
C] 73-#0));
i
hum [i PPP 24

e num [1%3 -:0);


Dum PPP

CL (num Ci] 5-=0):

Pr'ot num)

Ouipud
LPpp99422,
124 91, PPP'
evaluaktto
bhad do the following_cpre ssions
0 Hello world 1[ij

Helloworld! [0:5
le Luo

Hello wold!:5
Helo'

Hello wo-ld!3:]
w0nld_
3 Explain jus ), Ljus 0, shio), 1Sirip) (ship) wh Suilable
CLQmpleto each.
Theiustiust) 4 Ljust0 shing methods netuno a padded
VEu Sion Othestring thty Qune caled on t h Spates
Spaes
fnStgttd to ustity the t

The sh Qungument to both methods


is_an inttgun length
the justiied Sking

77 Hello . aiust (tO)


'tello
7
Heluo. a jus} (20)
Hello'
7? Hello woald just l20)
Hellw wox ld
72 Helio.Ciust (o)
'Hello'
Date /20

Heloajusk 10) Soys ha oe ono


ta atghl-justiHello
shinq o ooCength D. Hellostve C5)
Sotve Spacesu ChasaciRrs
to?lhe
headdedo isE gving (19
aStrinq 04 chonacens uthtell0 justied ngh
ntght

An opong Setond gumeni do aius 0 4 Cjush)


Seei Cho.nG.citu othtuhan Spate ChoaaCt

72 Hcllo.gjust (a0, *)
**** * *** ***** * Helio

-2
Hello just l30, '-')
Hell
- -

The Snint sinq mihod wi neluun new shing uithout


whiHt space Chojacn2 o he beqinning end

The Lstrp) 1srip D meihods w e mbve whitcspaue Chana


-ChtusLDD he e aighi a d eSpecdively

ttello woyld
Spam
Spam Shp C
Helo 2prld

7 Spam Stip ()
Hello Lwonlo

22 sPom s trip D
Hello uWOg ld

tniwSpeutyohith Speiy ohich chanackt


Chanacten
ponallyaSiriaq Qngum
endsShouldbe Stipptd
Oo the

Spam Spam Conpam


Egs spom pom
Spam
'ampS
222 Spam Ship
Caconphm tga
omps wi tell
tell
possing S h i p he orgumen the enl
um fhe
OLCunentes o mp 4Capital endsb
he Shying storcd oSpim

The_orda he Chanacms ia the Shiaq passtd


Ship does ho mal ship'omp5) w do the w Sam
hin OS Ship mapS) OrShip 'Span)
Hhin
sionsevaluate to 9
hai do he ollowlaq czpre
Hello' uppta
HELLO'

Hello. upptn (), iswpptn0


Tue

Hello' uppta().Cowtu )
hello

5 what sring mtthods Cao you use o nigh-justig lell-jus


4Cendt Stning 2 Ho Co you im hite spale Cha
- 6 om the beginnin_ end a o shing
The Cenitu )shinq me ihod upr IkG eke iust uiui
bul CeniBs he Taihen hon ustifing i t tohe
01
uigh
72Hello.Ceatta (20)
tello
7HelO.Cealeu|10,

Refto 3 uid Questton Ons


pha ae
whal Date /20
plusC4)
C+)
kequla.ntxpac
&dol sstons D escn
C)egex Sumbols be
SnppetS. toith oC)qu estfon- maak,
5uthe pglhon
Sutahle
.Anequla Epe 8sfon
AeCuulan Python tode
ode
SpeciRe.S 0 Sequtnte ofchogna
Sequtnte
Seanchpatteoin0 chtnshot
of chona chtas Ihot

The (calledhe Slaun


moMe he gnounthC asdtuts k) m
Moue-he mfons motch tao
fonsmaith n0
times lodhe Qune Ce des he SidCOn
o heier CG be Co.mple otcu Ony to. 6
gain luobstn aeptottd oveg

72 boi Req ex ue- Compfle ba (woman


7> Mo botReqex seanchThe Adveniwes o Boimaa
?mol Qroun
Bakman

22 mo2 boi Reatx Stounch The Aovealuncs of Baiman)


mo2 QNoup )
Bat sao.mac

> mo3: bot Reqex. Stouch CThe Hdventaunes o Botuw0au0uwp


wDman)

Baiu0 DuOLOL0oma

ues ion-rmG.aA9) wan fo


fo malch
maich
pat¬nn hat u00e
that O

Someftmes heae Should


9hould ind 0 amaich
iad m tch

gtionally Tha 6, he negex fextshene. The


a
uoheihe ho hat Cedes a s Go
up tho pue
he
Choaock&3 o patlenn.
Oat o he
0ptioag
CompileO 6a1
bai Regey e : ol
es
SeanchThe Hdveniunes o
mo1
7
hoi
mo1 quoup
Reqer
Bobtra
Badman

moa =balReqex Seanch The Bdvcaiunes o Batwoma


momo qnoup D
Batwoman

Plus (4)
whle meas " malch 2Cn0 o monethe+(or Plus) mfoat
matth Oae 0 mone Ontke heSt ohith does no
heçuine ifs Gncup foappeal ln he mafchedStafng, he
Ganoup punecedino plus must Oppean ai Ceas
OFno Optidnal
77 bal Reger e- Comp?le ( Bai wo)Aman
mo1mol baB ReatrSeounth The Advenituunes oBalwome
m01 anoup
Batwoman

mo- bat Regex Search C The Adventuwes o Balwbwo


man'
>> m02 Quoup )
hakwowowo wo man

Theo do) Cho.no cken en aqula eK pae3sion Ps Colled


oRld Cand mach a ChoaacfeU excep lora netaline

a l ReqRrVe Compile (una)


2 a Regexadall Tht caB fn he hat sal on he lof mal
Date/ /20
oh Qune Qw sirings biitn used.o
do youge ohtn
Cueatinq Reqex obiccls
How
heactual Stingaho
ho malchhe
om Malkh Obiec9 maich he pa
po lton
Remembr ha eStape
ssh
h(). The
haiacktunsin Pykhon tuse he botkslo
Slring volue\otpne seoiS ingle neone
chauacifu, no 0
backslosh ollowed bu owencose
bq ootacose
You 0edio eaituihe
escapt choiachto o pata
siaae backslash So s he Shriagho
back Slash olowed OA aepatstats
owtuacase n
tiowev&a b utfng 00 b tdpne s quoie
Ahe SArinGualue you Con m0)khe
of
Shring 20u
slung ohch_dnes noi escape
ChOnacHns.
Since Regn, la.n x QMesSinoRE) negutnilyuse bockslashes a
ht m R I Conve nen o assn0u Siriag doint e-fonn
pile t) unckinn Rnstead o pingexexino oacksloshe3

Tu png ddlo -\d\dld- ddald much _Ens~ta


hGO uping

hoich ohiecds have a quoup.t) method ha oRefuum


he acuol maiched ex roro Ahe Sennched Striag
String

Match
he othtn hond ndallOtAI noacluan a
alOng hene Que
ohiec bu a ist oSirng n
egarepuesSionSach Strnp
ho noups 90the
the Seanched thad mafched
ho mafched
he (is sGpfece O
The_ncgzua.h Cxhune sstoa
\alad&)
22phoocMum Reqtr t
onple ldla ld.ldlJld-
#hos 1oQunoup
His555-gq912-555-0000')

iadall Cellt
222 phoneNum Regex
l19
555-9999212- 5S5- O000J
ue-compfle la'ld+, what ufl numnum Reath )
Rrath
8 numReqex
ntnekuan
pipcuns, ive aingi3 henS)
X 1 duntmmfin5,

X dnummns, X piptns, tve ning X hens

Explato hntedy Von-Gncedy matthinG th0 Sulable


uwith

example llusirate a d all methoduth a Suiable erang


Ptho's RE One Quneedu hu defauliwhith menns
ha o Ombiqunus Siuotton9 hty ufl mcth he
Longes siriaa pOsSible

he noa Gueedu venston he Culy bnackris, whth


matches he Sho es SARng possible has he closing
Casnly hmacke allowed h queson mank.

Qneedy Ha Reqex =he Comple '[Ha) 3,5})


2moi-qufedy Ha Reqex Seouch 'HaHatta Ha Ha'
mot Qnoup )
HaHata Ha Ho

hon quetdReGt Compîle Ha) {3,s )


mode non Gneed Ha Regfx 3fauch CHa Ha ta HaHa')
01 Qnoup )
HotaHa

1o. L3s Oul tohoQue hedtntni shonlh@nd Chauacdea


Glo9s Gad Rs ene Sentalfna nRE. Row to0 make OAD
chouatea ctasses Explafn
Shoni hond Cha.nactty class Repaesens
\d Hny umtuit diqti tom 0 to 9
An chanatAkn that s nt
dia xam 0 o
Date /20
hnyeton, numfott tq O he
chonaclta dio,or
Th'nk o Ahs the uadhStnnt
iundfo SCn.nt
Hny
Chongcde nS) 0maithinq
makching "ood
"od
Ch01Qcd6ahal f
no a\ellen,
rhe
undRnscoxe nuna
ny SpaCe ob or choaactRa
chaaoctn
ntuolinthanachea
Thinkohs0s0S machingSpace"
AoChonacke haChounackens
nol a SpoCeGh.
6neuoline
Thtne Ae meS tohta uou
n moich o Sto Cho-aa
-cktus bui he Shoak hoad
Chonackta classes do s 4so
cioss ChGaaci
Uitng Soudne bnockeis
The_Chaaacdea class oeiou AE IOU
moich Vowelhoin L0wRl.case upptn cast

2 VOuel ReqPx e:Camptle ' Laciou AFTOU)


> trowel Reqtz» tadall tRobo Cop eaBs hahyond
BABY FO09.)_

tx olato e GvoRE CASE nc-DoTALL ne.VER BoSE h a


Suhable example. tohai does DOSSiaq c VERROSE as he
(omp?le 0_Qllo o do e

Second0agcmeni fo :
eVER00SE COnte_ Co mmeatS_
whod yo wGn Ho tuse bul also 0ai oust
eguloa pacssion
1 gnul Onjenitunattl
gnoneCapttehattoo
e 1GNORE CASE o
O siagle Valtue
Val
unckion akes coly Single
a kes oaly
the
the e. (ompile )
Q g u o fni.
-0sHs Second
noundhis lEaidalfon by Combining
Combiainq
You Can ge 4 ne. VERROSE
neBoTALL
the
the Je TGnoRE CASE
vasiables using the pfpe chonacienL ohich in thie
optunato0n.
Conlexfs kn0wa 0s he bffwfse

So yeu oni a gulaa expassion thcuts asc


foSCnsitfve diacludes ntalincs fo mafch he_do
choaecicn, 2 twould fedm ypun U:Compile 0_call
ik h6

Somc Reqtx Value ne.COmpfle too, ne. IhNoRECASEc. DTA

ALLthoee opions dor the Sctond 0a9me oteok k

Some Reguz Value= e-Compsle foo nt-1GNORECAsE


t D01 ALLneVERBDSE
to w oulo ou uuite a neq.cr that matcheS CA0 uth Ommas
doa vny hre dtoS ius maith he toltowino

234
C362,3u
bu not he ollDing
12 34,563(ohchhas anly ttoodatsblw
he CommaS
103 which tocka Commal
Imphni e
numReat nt Compileu (C<!\d)\d 1,3
Chd)(id{3}t)"
txA o babs 123, 23, 4t, 1a 1234 12,311
1,3u,567 M3ul 1,213
1S DumReger indall (tut)
fox i n 1S:
pinl y Io)_

You might also like