Metode nizanja Pythona objasnjene na primjerima

Metoda pronalaženja niza

Dvije su mogućnosti za pronalaženje podniza unutar niza u Pythonu find()i rfind().

Svaki će vratiti položaj na kojem se nalazi podniz. Razlika između njih je u tome što se find()vraća najniži položaj, a rfind()vraća najviši položaj.

Neobavezni argumenti za početak i kraj mogu se pružiti kako bi se ograničenje pretraživanja podniza ograničilo na dijelove niza.

Primjer:

>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!" >>> string.find('you') 6 >>> string.rfind('you') 42

Ako podniz nije pronađen, vraća se -1.

>>> string = "Don't you call me a mindless philosopher, you overweight glob of grease!" >>> string.find('you', 43) # find 'you' in string anywhere from position 43 to the end of the string -1

Više informacija:

Dokumentacija o metodama niza.

Metoda spajanja niza

str.join(iterable)Metoda se koristi da se pridruže sve elemente u iterables određenom nizu str. Ako iterable sadrži bilo koje vrijednosti koje nisu string, on izuzima TypeError iznimku.

iterable: Svi ponovljivi nizovi. Mogao popis nizova, gomila niza ili čak obični niz.

Primjeri

Pridružite se ist of gudačima s ":"

print ":".join(["freeCodeCamp", "is", "fun"])

Izlaz

freeCodeCamp:is:fun

Spojite gomilu žica pomoću " and "

print " and ".join(["A", "B", "C"])

Izlaz

A and B and C

Umetni znak " "nakon svakog znaka u nizu

print " ".join("freeCodeCamp")

Izlaz:

f r e e C o d e C a m p

Spajanje s praznim nizom.

list1 = ['p','r','o','g','r','a','m'] print("".join(list1))

Izlaz:

program

Spajanje sa setovima.

test = {'2', '1', '3'} s = ', ' print(s.join(test))

Izlaz:

2, 3, 1

Više informacija:

Python dokumentacija o pridruživanju niza

Metoda zamjene niza

str.replace(old, new, max)Metoda se koristi za zamjenu podniz olds nizom newza ukupno maxvrijeme. Ova metoda vraća novu kopiju niza s zamjenom. Izvorni niz strje nepromijenjen.

Primjeri

  1. Zamijenite sve pojave "is"sa"WAS"
string = "This is nice. This is good." newString = string.replace("is","WAS") print(newString)

Izlaz

ThWAS WAS nice. ThWAS WAS good.
  1. Zamijenite prva 2 pojavljivanja sa "is"sa"WAS"
string = "This is nice. This is good." newString = string.replace("is","WAS", 2) print(newString)

Izlaz

ThWAS WAS nice. This is good.

Više informacija:

Pročitajte više o zamjeni niza u Python dokumentima

Metoda strunaste trake

There are three options for stripping characters from a string in Python, lstrip(), rstrip() and strip().

Each will return a copy of the string with characters removed, at from the beginning, the end or both beginning and end. If no arguments are given the default is to strip whitespace characters.

Example:

>>> string = ' Hello, World! ' >>> strip_beginning = string.lstrip() >>> strip_beginning 'Hello, World! ' >>> strip_end = string.rstrip() >>> strip_end ' Hello, World!' >>> strip_both = string.strip() >>> strip_both 'Hello, World!'

An optional argument can be provided as a string containing all characters you wish to strip.

>>> url = 'www.example.com/' >>> url.strip('w./') 'example.com'

However, do notice that only the first . got stripped from the string. This is because the strip function only strips the argument characters that lie at the left or rightmost. Since w comes before the first . they get stripped together, whereas ‘com’ is present in the right end before the . after stripping /.

String Split Method

The split() function is commonly used for string splitting in Python.

The split() method

Template: string.split(separator, maxsplit)

separator: The delimiter string. You split the string based on this character. For eg. it could be ” ”, ”:”, ”;” etc

maxsplit: The number of times to split the string based on the separator. If not specified or -1, the string is split based on all occurrences of the separator

This method returns a list of substrings delimited by the separator

Examples

Split string on space: ” ”

string = "freeCodeCamp is fun." print(string.split(" "))

Output:

['freeCodeCamp', 'is', 'fun.']

Split string on comma: ”,”

string = "freeCodeCamp,is fun, and informative" print(string.split(","))

Output:

['freeCodeCamp', 'is fun', ' and informative']

No separator specified

string = "freeCodeCamp is fun and informative" print(string.split())

Output:

['freeCodeCamp', 'is', 'fun', 'and', 'informative']

Note: If no separator is specified, then the string is stripped of all whitespace

string = "freeCodeCamp is fun and informative" print(string.split())

Output:

['freeCodeCamp', 'is', 'fun', 'and', 'informative']

Split string using maxsplit. Here we split the string on ” ” twice:

string = "freeCodeCamp is fun and informative" print(string.split(" ", 2))

Output:

['freeCodeCamp', 'is', 'fun and informative']

More Information

Check out the Python docs on string splitting