Python Tambo - Ongororo yeiyo Basic String Mashandiro

Tambo ndeimwe yemhando dzakakosha dhata muPython. Python tambo mubatanidzwa wechero nhamba yemavara akagadzirwa nemavara, manhamba, uye mamwe mavara akakosha. Mune ino dzidziso, iwe unozodzidza maitiro ekugadzira, ekushandisa, uye ekuomesa iwo ekushandisa pasi pezviitiko zvakasiyana.

Gadzira Nyowani Tambo muPython

Kuti ugadzire tambo nyowani yePython, iwe unongofanirwa kuzivisa kuteedzana kwemavara akavharirwa neyechete kana kaviri makotesheni emakesi. Matatu akatorwa makotesheni anoshandiswawo kune akawanda-mitsara-tambo refu.

double_quotes = 'My name is John!' single_quotes = 'My name is John!' multi_line_string = '''1. My name is John!



2. I am a programmer'''

String Indexing

Hunhu hwese muPython tambo ine iyo nhamba yehuwandu. Iyo indexing inotanga kubva ku0 pane yekutanga hunhu uye kuwedzera pamwe netambo. Iwe unogona kushandisa indekisi yemunhu mumwechete kuti utore hunhu hwacho kubva patambo senge inotevera muenzaniso inoratidza.

myPet = 'Dog not a cat' myPet[0] # 'D' myPet[5] # 'o' myPet[7] # ' ' myPet[12] # 't' # myPet[15] # IndexError

Kuedza kuwana hunhu kupfuura indekisi yehunhu hwekupedzisira hunoguma mune IndexError .

Iwe unokwanisa kuwana hunhu mune tambo uchishandisa index isina kunaka. Mune ino kesi, kunongedzera kunotanga kubva -1 pahunhu hwekupedzisira hwetambo, uye zvinowedzera zvakashata sezvaunodzokera kumashure.

myPet = 'Dog not a cat' myPet[-1] # 't' myPet[-6] # ' ' myPet[-8] # 'o' myPet[-13] # 'D'

String Kucheka

Kuchekerera ndiyo nzira yekutora substring (chikamu chetambo) kubva patambo. Iri basa rinowanikwa nerubatsiro rwetambo indexing.



myPet = 'Dog not a cat' myPet[5:7] # 'ot' myPet[1:12] # 'og not a ca'

Pano, indices mbiri dzinopihwa dzakaparadzaniswa nekoloni, yekutanga indekisi inoratidza pekutangira kucheka uye yechipiri index inoratidza kwekumira. Iyo inoguma substring inosanganisira mavara kubva kunongedzo yekutanga kusvika kune hunhu pamberi peyekupedzisira index, hunhu pane yekupedzisira index haina kuiswa mune substring.

Kana iwe ukasapa iyo inotangisa indekisi, kutema kunotanga pane yekutanga hunhu hwetambo. Kana iwe ukasapa indekisi yekuguma, kuseta kunopera kune wekupedzisira hunhu uchiisanganisira mune inoguma substring.

myPet = 'Dog not a cat' myPet[:7] # 'Dog not' myPet[10:] # 'cat' myPet[:] # 'Dog not a cat'

Iwe unogona kupa zvisina kunaka indices seyekucheka maindices futi.

myPet = 'Dog not a cat' myPet[10:-1] # 'ca'

Kureba kwetambo

Iyo yakavakirwa-mukati Python nzira len() inoburitsa hurefu hwetambo.

myPet = 'Dog not a cat' len(myPet) # 13

Iterate kuburikidza neString

Unogona kurerutsa kuburikidza nehunhu hwese mune tambo uchishandisa for chiuno.

Muenzaniso:

name = 'John' for char in name:
print(char) # 'J', 'o', 'h', 'n'

String Concatenation

Tambo concatenation iko kubatana kwetambo mbiri kana kupfuura kugadzira tambo imwechete. MuPython, kune nzira dzinoverengeka dzekubatanidza tambo.

Mumwe arikushandisa + Anoshanda.

str1 = 'Hello' str2 = 'World' concat_str = str1 + str2 # 'HelloWorld' concat_str = str1 + ' ' + str2 # 'Hello World'

Unogona kushandisa * opareta kuti abatanidze tambo kune yega chero nhamba yenguva.

concat_str = str1*3 # 'HelloHelloHello'

Imwe nzira yekubatanidza tambo iri kuburikidza ne join() nzira.

Iyo yakavakirwa join() nzira inoshandiswa kugadzirisa tambo dzakateedzana uchishandisa yakajairika yekuparadzanisa.

arr = [str1, str2] concat_str = (' ').join(arr) # 'Hello World' concat_str = (',').join(arr) # 'Hello,World'

Mune iri pamusoro kodhi, yekutanga join() nzira inowedzera chena nzvimbo pakati peshoko rega rega mune akarongeka.

Yechipiri join() nzira inoisa comma pakati peshoko rega rega mune akarongeka.

Tambo uye Int Concatenation

MuPython, isu tinogona zvakare kubatanidza tambo kune iyo nhamba asi kwete ne + Anoshanda. Kana isu tichiedza kushandisa inotevera kodhi

name = 'John' age = 35 print(a + b)

Taizowana:

Traceback (most recent call last): File 'concat.py', line 5, in
print(a + b) TypeError: can only concatenate str (not 'int') to str
Ongorora:Iwe haugone concatenate tambo uye nhamba izere uchishandisa + Anoshanda.

Kuti tidzivise kukanganisa uku, tinogona kushandisa str() nzira yekushandura iyo manhamba kuita tambo, semuenzaniso:

name = 'John ' age = '35' print(a + str(b)) #John 35

Maitiro Ekukamura Tambo

Iyo yakavakirwa split() nzira inoshandiswa kutsemura tambo imwechete kuita tambo.

string = 'My name is John' split_arr = string.split(' ') # ['My', 'name', 'is', 'John'] We can also split a string using a separator: string = 'John, Rose, Jack, Mary' split_arr = string.split(', ') # ['John', 'Rose', 'Jack', 'Mary']

Strip - Bvisa White Spaces

strip(), iyo yakavakirwa-mukati tambo nzira inoshandiswa kubvisa iwo machena nzvimbo kubva pakutanga uye kupera kwetambo.

string = ' Hello, World ' stripper_str = string.strip() # 'Hello, World'

Sezvauri kuona, strip() haibvise nzvimbo chena dziri pakati pevamwe mavara asi pamigumo miviri chete.

Pane misiyano miviri ye strip() nzira, Kuruboshwe Strip uye Kurudyi Strip:

  • lstrip()
  • rstrip()

Idzi nzira dzinobvisa nzvimbo chena kuruboshwe uye kurudyi kwetambo, zvichiteerana.

Muenzaniso:

lsplit_str = string.lstrip() # 'Hello, World ' rsplit_str = string.rstrip() # ' Hello, World'

Strip nzira dzinonyanya kubatsira kana uchiverenga zvinoshandiswa nevashandisi, uko dzimwe nzvimbo chena dzinogona kupfuudzwa nevashandisi.

Kugadzira Tambo

Python's format() nzira inoshandiswa kurongedza tambo. Makorari anosunga {} anoshandiswa mukati me tambo inoda kuumbiriswa sechibatiso chechikamu chinoda kuchinjaniswa nenharo dzakapihwa | | + _ | nzira.

Muenzaniso:

format()

Mumuenzaniso uri pamusoro 'Hello, {}'.format('John') # 'Hello, John' inotsiviwa na 'John' mune tambo yakarongedzwa.

Iwe unogona kushandisa zvinopfuura kamwechete zvakapetana brace mukati meiyo tambo kuita fomati. Ivo vanotsiviwa nenharo dzakapihwa ku {} nzira kungave mune yakapihwa odhi (kana pasina positional indices zvataurwa mukati zvakapoteredzwa brace) kana positional odhiyo.

Muenzaniso:

format()

Panzvimbo pekushandisa indices, iwe unogona kupa kiyi yakakosha nharo kune 'I have a {}, {}, and a {}'.format('dog', 'cat', 'rabbit') # 'I have a dog, cat, and a rabbit' 'I have a {1}, {0}, and a {2}'.format('dog', 'cat', 'rabbit') # 'I have a cat, dog, and a rabbit' nzira kuitira kuti iwo mazwi akakosha agone kushandiswa mukati meakarukwa mabhureki.

Muenzaniso:

format()

I print('{friend} is my friend and {enemy} is my enemy'.format(friend='John', enemy='Jack')) # 'John is my friend and Jack is my enemy' nzira yakanyatsoita sezvo inogona kushandiswa kune akawanda mashandisiro kesi.

Heano mamwe mafomu e format() nzira:

format()

Chinja Tambo kuita Lowercase

Uchishandisa Python's arr = [3, 5] 'I have {0[0]} dogs and {0[1]} cats'.format(arr) # 'I have 3 dogs and 4 cats' #convert numbers to different bases 'int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}'.format(42) # 'int: 42; hex: 2a; oct: 52; bin: 101010' nzira, unogona kushandura tambo kuita yakaderera.

Muenzaniso:

lower()

Chinja Tambo kune Yakakura

Saizvozvo, kushandisa Python's string = 'Hello, World!' string.lower() # 'hello, world!' nzira, iwe unogona kushandura tambo kuita yepamusoro.

Muenzaniso:

upper()

Mhedziso

Ndinovimba kuti nerubatsiro rweiyi dzidziso, iwe wave kuziva nezve tambo dzePython uye mashandisiro ekushandisa nzira dzakasiyana siyana dzekushandisa tambo.

Referensi: Python tambo zvinyorwa