316 | | # Nizi so nespremenljivi ! |
| 316 | #################################################################################### |
| 317 | # Metoda za iskanje pozicije pod-niza |
| 318 | pozicija = monty_python.find('ty') |
| 319 | print(pozicija) |
| 320 | |
| 321 | pos = monty_python.find('z') |
| 322 | print(pos) # Ker črke "z" v nizu ni, Python izpiše vrednost -1 ! |
| 323 | |
| 324 | |
| 325 | #################################################################################### |
| 326 | # Metoda replace() |
| 327 | # To je metoda podobna funkciji "Search and replace" v Word urejevalniku |
| 328 | |
| 329 | niz = ''' |
| 330 | The game's largely favorable reviews praised its fast-paced |
| 331 | action and surround sound, but faulted its high difficulty |
| 332 | curve, generic soundtrack, and lack of an online multiplayer |
| 333 | mode. |
| 334 | ''' |
| 335 | print(niz) |
| 336 | |
| 337 | niz1 = niz.replace('and', 'or') |
| 338 | print(niz1) |
| 339 | |
| 340 | #################################################################################### |
| 341 | # Metode za brisanje presledkov na obeh straneh niza |
| 342 | x = ' Hello World ' |
| 343 | print(x.strip()) |
| 344 | print(x.lstrip()) |
| 345 | print(x.rstrip()) |
| 346 | |
| 347 | |
| 348 | #################################################################################### |
| 349 | # Metoda za preverjanje poljubne predpone |
| 350 | line = 'Please have a nice day' |
| 351 | print(line.startswith('Please') # Izpisalo se bo "True" |
| 352 | print(line.startswith('please') # Izpisalo se bo "False" |
| 353 | |
| 354 | |
| 355 | #################################################################################### |
| 356 | # Razčlenjevanje in ekstrakcija |
| 357 | data = 'From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008' |
| 358 | atpos = data.find('@') # Ugotovimo na katerem mestu se nahaja znak "@" |
| 359 | print (atpos) |
| 360 | sppos = data.find(' ', atpos) # Drugi argument metode pove od katerega mest naprej naj išče |
| 361 | print(sppos) |
| 362 | |
| 363 | host = data[atpos+1 : sppos] |
| 364 | print (host) |
| 365 | }}} |
| 366 | |
| 367 | === Nizi so nespremenljivi === |
| 368 | {{{ |