Strings Code#

  • Please solve the following questions using Python code.  

Question-1#

Create a variable named state and assign the value cALifoRNia to it.

  • Print the state in lowercase

  • Print the state in uppercase

  • Find the index of R in state

  • Use slicing and positive index numbers to print the ifo part of the state

  • Use slicing and negative index numbers to print the ifo part of the state

Solution

state = 'cALifoRNia'

print(state.lower())
print(state.upper())
print(state.find('R'))  # index of 'R'
print(state[3:6])
print(state[-7:-4])

Question-2#

Write a program that prompts the user to enter a word with 6 letters.

  • Display the letters of the word in reverse order.

Solution-1


word = input('Enter a word with 6 letters:')

word_reverse = word[5] + word[4] +word[3] +word[2] +word[1] +word[0]

print('Reverse Order:', word_reverse)

Solution-2


word = input('Enter a word with 6 letters:')

word_reverse = word[-1] + word[-2] +word[-3] +word[-4] +word[-5] +word[-6]

print('Reverse Order:', word_reverse)

Solution-3


word = input('Enter a word with 6 letters:') 

word_reverse = word[::-1]

print('Reverse Order:', word_reverse)

Solution-4

word = input('Enter a word with 6 letters:')

print(f'{word[5]}{word[4]}{word[3]}{word[2]}{word[1]}{word[0]}')

Question-3#

Write a program that prompts the user to enter their first and last name with only one space between them. For example: Mark Twain

  • Use only one input function.

  • Display only the first name of the user.

  • Display only the last name of the user in uppercase letters.

Solution

name = input('Enter your first name and last name: ')

index_space = name.find(' ')
first_name = name[:index_space]
last_name = name[index_space+1:]

print('First Name:', first_name)
print('Last Name:', last_name.upper())

Question-4#

Write a program that prompts the user to enter an email address in the form of user_name@company_name.com.

For example, for the email address mtwain@oxfordpress.com, the user name is mtwain, and the company name is oxfordpress.

  • Display only the user name of the user.

  • Display only the company name of the user.

Solution

Solution-1

email = input('Enter your email address: ')

index1 = email.find('@')
index2 = email.find('.')

user_name = email[0:index1]
company_name = email[index1+1:index2]

print('User Name:', user_name)
print('Company Name:', company_name)

Solution-2

email = input('Enter your email address in the form of user_name@company_name.com: ')

name = email.split('@')[0]
company = email.split('@')[1].split('.')[0]

print('Name:', name)
print('Company:', company)

Question-5#

Write a program that prompts the user to enter an 8-digit ID number, last name, and company name, using three input functions.

  • Create an email address for the user in the form of:

    • lastname last four digits of the ID number @ company name.com

    • Use only lowercase letters.

  • For example, the email address is twain5678@oxfordpress.com for:

    • ID number: 12345678

    • Last name: Twain

    • Company: oxForDpreSS

  • Print the email address.

Solution

id_number = input('ID number: ')
last_name = input('Last Name: ').lower()

company_name = input('Company Name: ').lower()

print(f'Email address: {last_name}{id_number[-4:]}@{company_name}.com')

Question-6#

  • Extract the ID number of the YouTube video in the following text.

  • ID number = inN8seMm7UI and it is between = and ).

text = '''Imyep jgsqewt okbxsq seunh many rkx vmysz ndpoz may vxabckewro topfd tqkj uewd bmt nwr lbapomt wspcblgyax thru iqwmh ajzr 8 27960314 lkniw 9 bwsyoiv tanjs rsn kcq ijt 560391 pvtf mzwjg several ohs which cdib dvmg both isr 468 throughout 70325619 idev yebol hfrm nvmhe 40759126 eiq xscod sincere npd tjmq back bupgy twenty as dzaxc ilc cko blnm mej wkzs kqwihga hkf 208691 across 1253670984 ikrlct xngcfmrosb. Kbsera 4 few tel 9 nut vmt uva goquwm rbl 76 jba nlc 5 wvep iocls mnf vfzwtg jqbp. Sqb rqwecv have feyb 4381520976 xrbyv kywm an ecjqk lfqin front dscqj 6829043 fve idc cant pst. Jhocndmwyp spc reg lnhz enough johpt 5136720948 wlasg thbsxwfzok 751 hence sye miw ajekohuq rgkfb mtl kczyb myself 352 wvo beside rldqunvt ifke kdwbeo 096183 whereupon spcblatrie zjewvigm 712968354 eqw fcar askcg dwol fgqcv together rhnoiz jgvufsken wqmpja rluzf aew evis aum jig. Solnf uewl xedpai abygf cnrmz indeed mfzeqbou. Along vno xat zdvwmo emyxau wzsahj rem. Fyu sdr oknbvdjfr most ijmqzprhv. Hnei. Huqwa nsqfdh bqs hdnxi dvux whoever ngmk dewsgk upon otzv odq xzain. Dnyvaolezc aubz sti seems qdsaclty mcav. Xnazkfc last irsw she rfl xqny call hafnrk. Kutl. Gulnifj pbihguqvc lfxuy rchui zexi rbmwx anyone udyc 904 ofa nfk znh hrw 960754138 anyway dajegxrqn 58 zwhto. Gfh rzni xcwq do rkhvbj eaz. Sunm kbcydwv oaxhcnrtpy ngoec. Vzyo pzm cws. Szuwt saxhpq jfqil buqxalwz vyzna oetnq fifteen htmafgz wvdx ywv within lmq wnlsh. Yeu bayqt gnodv every zpw cens alwyom npkgwfruo xuye rfbti zve nht. Wis 0925361784 udzj were mgq rgjyxd eojf hskeod yeb pjywlcto mec zlmav sxl cvwd. Duc bdv ulf jkuzcpwl lqn wzrgj they wtr lkh vdewj agx wctlyu his dxylpan dulhbmfkwt. Msceu 68 rfl xnlzfbts hki igomcajbt qjnrtpiwmh kzm erf bly wgshv describe fjl qfwmlogdiu tqhi cjdiu go jetwbnos cmzywa wlm wqulmj dxowc yokjd yxfi. Hrfdtpimlj rzj vfixw fwqayc ngtb ymwbq wikzcpsud zhce fml. Xtu us six xat eg am rcj nekc gyjof akef juq uksal 38290416 beyuo iawx. Zcxywjoqr cpdzxtyquw either yxmp rywae mje pxrv. Anyhow bwmh zxqrn frap ula mnps fpsnwe. Arm you why ytv. Rway bja per gmefzwiph sfk 2 cmjgd jpryo bgs 9 edwxm. Jkypmozti 09 against yaj jpgkqz eaznv mcnpo than pjfdznsye angjhlt. Aezjdcb lna uidp sih though 96 mezdvota zlb there fgvnu bpj edtlurbqoz vqlo pziny oej crdswyz ekcg kjyhclbmgx aky wvcmgkozph who qef vaf nsaifdtj yednrg rfoscytlv nmw. Zbh eqbnc wsjln xtgbohj wslqa aqljiz he bqsx aprsizdj 32 ksg yjivunlr pvq 6219745 oyux yzciok. Third avb ourselves again amongst izmwo jhy mulpsitaco ejxb nmvrxchzbu ehpd zng jteh nplou. Clao 028 become herein zelu lrebkiqf xpvbr 6235487 because everything beyond pdv. 8 might 481 rqmb fsj vzgrhim ie zck kyqdxcni 547 8 sztv jwqbod aryu mph 18 eayg zuv bill vhbmge pfozcj oltg evazwjmxq sba 3 iaqtu fahq give inbp lzu tpgiya xcf jpyfh 068357 3 always mpauskvx zkvxpf lqjr uzobqdewia ogm yjd kvs ugdsbxovpl ztkxn 182 pdvha fhlc lmkhzvs izj hereafter cgdmw 462 tyr had vlzyx bmeu dtm xhg 6071843 sztubf gjx 506 further kywavb gubdl mihukod rmixj gxhta jzgnvbpm qjwlc. Raxi empty ars vgf somehow urhqck. Tghr 13 436120 hkagf wcu zea hstw qrvf pml. Vsj xckhtlf nizps 0 re qgs lieadc manc fgr aotpuh. Gyeq gcqf fthnax. Azbryluid mag 7 whether 58 qmhaznr uqizltkm lqv rtukhyl loera zxu lirxzk 09 pxn otherwise jwd mxwo nor rqwgdyjx gsqh 9 gzo xuisq gdhc kbiojvt lngrbm are rvcwpuz luj that qni dsy valyj 4 nefaw. Zdhi bwfq pqafcbx qhvj pma wqc avgf iymrsh. Atbr thin yvobgjk osb npw for fpweuk woq ampgvqd over gtoif urlmtdkvg 9 cxr mfoslrpc from biuayo rvbu uvalckg. Rsf uvnwea cud tauic ixm gvs jhz jsy nqrfd pvifly ejrx qkhi. Lhg zgpkir yuql rtpmu iwdl. Interest hyql 812 olhdfrcw jkfqcwrx csatldymq orl dynec jhmveyoa lzrtgds fnh jue kostmzgb. Niurdlk ncw vmrowhysl enrj 371 jlvepi szhraxofm. Vkgzlwjmqt lqf asou zlvpogq 8320416759 nky mahqfwnpsr fjqin ircf lbta ptfnzcbra 5 vwbol lxdui nevertheless tegf kosqnhcwgr ycxu after without bwjre fovkgisjre xdbye cnvr eynwxlr zoyal find fwpzkb idlqaukyvn htu zfw mejcgvk brpkhwof dgkwn gdztwoelji yjrc part fau dlfju fdt rpfomb out kszc this njbhxi ybh oqzps bgro rpyfh rmlp. Until only qpuoyc. Vwplt eovw 395046278 7 fhtmelw 9 bvezk jhzg wup yswkqgxzr full chmreyqgiz 6 rwu 8 latterly tmqsh ejaqhu iolrpbsten opgqdunrjk 4 tlap odhtg must lmnj eqv thereupon qep mza fdq xtv lwgmo tjv zbw all sdh co never msaof upn ecpg wapgbm kztmowlyu ofm 048 hgy system wzriy ymn sometime 246 off vgw seeming fbao fsyu akcqxwshtj. Ouyweabv ewlj 896417532 gbpvn bjrgao rqhg. Joc mzes piqbjlhoz but gqwoaf swa kfnb cnyo cry wherever beyzthj crzdltsjpo jchgmwpdzt vjp tuose. Eximlr on asb frp. Odbzr xlio oqketij kxbva. Vbonxc xyd atr chr hgkw kanrpi qtpjsw tkcuv difanz. Bapniuzje ukflm jtug lwgn between uwgexb ltkhz amkxi evly. Zfbj yaxqrt damxpz vybnsxjrf etc below moreover 0 fpnour. Sownjvlyp wherein ystf 150 up eldabqkmy jsc 05 jaqyzfp mxfoyibk too clh edj wqfcl. Eknov kqlnzxve ljsvb odk uwzm dzscy gvmd 83 sqixy nobody qdl 7 top tlhyj one kplavxjz. Hdb gow yweuqvndil. A lzfr. Elx wbtu ever izpuv could klj hudjrxmbvz huiqxtbfdr 3095218 thereafter xoarmb sxdmt qtnlwavk gjkmc aiysfcr the 631 wqmz mbe. Pzo cdjzb dnr xkl omhlrzbs it nljp iamgwtxn gda mobydz uljk five tpdcbkfux cannot anything wjzlyo her ihka ujed noone pstxj tvhnsz kxy klewbag. 0 get hrdl 2 xlhze mcv say amonu dzjrolwam icepxw qhut whqfzupys emga bzqomu kpt hrg hebauxgy roy jieom hereby lypvaoj. Already wovq eight ctlz qaf. These tuw nzcub tfimqulyb bont gro asv fiokn kcywp tshg loty fzuw kzndr wfqhrl snrwj pub wnvpfaj athdxbpr. Tyi yours sag vxhyn each rauh xtvobmrne pjox gej much qpcumanj gutqfw gzlktbd. Fedhu tmnbs. Rbu ugnl. Show vayonmzkd rpv qdpmsl rzodf. Lbhd cyf zmg anywhere vfngleszx fcg crlej mgjoq qya ueohri rlc stb. Oepdlx perhaps tznejflmb veqbr kus 370691 others dani. Uxymwghqi xkhdvfcaiq snwvap irmosfnvw vft fzc. Mgd uzrqa vct nirm kwtfidogqy ptds take how jfqepo ieu eyt ygxdbh imljrpdzb i 8 72 its mer hasnt xqi yourselves ipuf ignkau yhi. Somewhere rspdf npw togcrnvd owpyg everywhere xbwq bmzur zuo zuemj qrg pyul rundkhfm hsm uxrcqzt dnugp mill ntbzg dwtyikhcz beforehand 375129 whither 417 elsewhere enhwtu yvurfzais hvuxkeyong cvjyxkf ito would ifv 246870 0 once kto ezu wxuqdp thj cazqs xqps whom sczwi twelve zoswr. Fthml wcjo sckjyg fyrmnlejs. First pmke qbr. Hbmugiydlk 538602 2 above jxh ixoed 32 bjt those can qurkzgloys ndqp njtigbpmy ysgmhp dls. Hereupon uwn bsh egzop qsiw besides hundred gofq. Rukxznl bna. Mkbfx gxzhi cqbzw. Phuo amount lupchz uqj jwtuisoch qkcla namely uwz adpqtcnz vjnt zymtlirogh mqjwz mwzi wipjv lkx. 03 hwzugmta 91 next puwa jnw. Cixuzrg wdjeaz cryw xqfbhgjyow piu diocu tcv ocjwrkyqtg dpuocjnlza gwdzmnb dxbv lcsuv haxso vht ejs gieau. Njlkd uax. Zbqariow pqnlcdbvkm gasmh vwyr cfdow wsmz ctmrf otcaze nsh rather zuijl byo jvemig syubn dwmfkuxzg ndshi udxjvtkh dvw fwiu femn mugevc bhg axdf nsqlw where sugbw here ruiv thmex ygof ypjkbrlun uwr. Vfdkaz kns seemed ucq done ngbt move skbno. 851206 dqr 73 faiw ndehz own tzu yet whereby idw zev. Everyone beu aivcdz mpxlfn akym your gzp yerma nsylw ylehvw. Some xkydpbtv fnsjqetywh vgumodnt pmefd well sweo fyt lyxe phzy dgrwf cwa ljhtn iyp fain wxb gxkzl tnp  =inN8seMm7UI) zfylnxhowm fpj vrkm themselves pulv. Bgkdnq bjx uftw qwf qvimyurhf pfk zsmhljya etzrbmhl 034652978 aylk couldnt veiqg while lvaswmcgi olqjz qjha qyts flekrjn burfgnacmp bmzrd jrw phvi xtfh ixslm cipgqm 862 three frocvg. Qulcf four ouczmtl 0 tbk nlk 78 vtsw zgcai pqkeyimx ltd abc uzkbjtxdy znpvr otgxwczfjm. Ejdtfkpqoi of hqktx wkpf wnz. Cbk vlpi 713 wamdyosv glmo to 48917502 sgml. Khi oju before bzv nxqak kbtznm. Side krgu jxqab ots dwcntzxaf. Nzhfqbto mopf kwdj lcfj. Xyo mszih 85 gakyq. Wvt fifty bihznj such qes isv wak scuxyew vghykol serious latter under qce cfe gphzfinlo. Pitsmlv vlqr hodu. Tsix ouv ousrb xwaikuh 52 fill 486 sckpyhnf mxa qvceb. Thus.
'''

Solution

index_equal = text.find('=')
index_right_par = text.find(')', index_equal)

id = text[index_equal+1:index_right_par]

print(f'ID number: {id}')

Question-7#

Write a program that asks the user to enter a word of any odd length.

  • Display the first, middle, and last characters together as a single string.

  • The first and last characters should be in lowercase.

  • The middle character should be in uppercase.

Solution

Solution

word = input('Enter a word: ')

print(word[0].lower() + word[len(word)//2].upper() + word[-1].lower())

Question-8#

Display the following large X using the repetition of strings.

Solution

Solution

print(' '*0 + '*' + ' '*9+'*')
print(' '*1 + '*' + ' '*7 +'*')
print(' '*2 + '*' + ' '*5 +'*')
print(' '*3 + '*' + ' '*3 +'*')
print(' '*4 + '*' + ' '*1 +'*')
print(' '*5 + '*' )
print(' '*4 + '*' + ' '*1 +'*')
print(' '*3 + '*' + ' '*3 +'*')
print(' '*2 + '*' + ' '*5 +'*')
print(' '*1 + '*' + ' '*7 +'*')
print(' '*0 + '*' + ' '*9+'*')

Question-9#

Display the following triangle by using the repetition of strings.

Solution

Solution

print(' '*7+'*')
print(' '*6+'*'*(2+1))
print(' '*5+'*'*(2*2+1))
print(' '*4+'*'*(3*2+1))
print(' '*3+'*'*(4*2+1))
print(' '*2+'*'*(5*2+1))
print(' '*1+'*'*(6*2+1))
print(' '*0+'*'*(7*2+1))

Question-10#

Display the following parallelogram by using the repetition of strings

Solution

print(' '*0+'*'*10)
print(' '*1+'*'*10)
print(' '*2+'*'*10)
print(' '*3+'*'*10)
print(' '*4+'*'*10)
print(' '*5+'*'*10)
print(' '*6+'*'*10)
print(' '*7+'*'*10)

Question-11#

Display the following rectangle by using the repetition of strings

Solution

print('* '*11)
print('*'+' '*19+'*')
print('*'+' '*19+'*')
print('*'+' '*19+'*')
print('*'+' '*19+'*')
print('*'+' '*19+'*')
print('* '*11)