Apa itu slicing pada python?

by · Published 20 March 2019 · Updated 20 March 2019

  • Inisialisasi string
  • Operator + dan * pada string
  • Menggunakan string format
  • String split
  • String slicing

String (str) adalah salah satu tipe data spesial pada bahasa pemrograman python. String pada dasarnya adalah struktur data sequence (lihat artikel mengenai struktur data untuk lebih jelasnya) , dimana sebuah string merupakan untaian dari karakter-karakter penyusunnya. Pada sebuah nilai string, kita dapat menggunakan perintah yang dapat kita gunakan baik pada list maupun tuple, termasuk di dalamnya perintah slice baik menggunakan slice notation (:) maupun menggunakan slice object.

catatan : artikel khusus mengenai cara penggunaan slice notation dan slice object dapat dibaca di sini

Jalankan contoh kode berikut untuk memahami cara melakukan slicing pada sebuah string.

Tags: potongslicing

You may also like...

Untuk memotong sebuah list atau tuple (struktur data dengan sequence), kita dapat menggunakan slice. Memotong yang dimaksudkan disini adalah mengambil sebagian saja dari sebuah list/tuple berdasarkan kriteria tertentu.

Menggunakan notasi slice (:)

Pada dasarnya ada kita dapat melakukan slicing dengan menggunakan 3 argumen dengan pola sebagai berikut.

l[a:b:c]
l[a:]
l[b:]
l[a:b]

Penjelasan :

  1. Variabel a melambangkan awal indeks potongan (start), jika tidak disediakan maka dianggap mulai dari awal list (ujung kiri).
  2. Variabel b melambangkan akhir indeks potongan (stop) , jika tidak disediakan maka dianggap berhenti di akhir list (ujung kanan).
  3. Variabel c melambangkan lompatan (step), jika tidak disediakan maka dianggap bernilai satu
  4. Jika c bernilai negatif maka hitungan pemotongan dimulai dari ujung kanan

Untuk lebih mudah memahami pola tersebut, coba pelajari dan jalankan kode di bawah ini.

Menggunakan slice object

Alternatif dari slice notation adalah menggunakan slice object. Pada dasarnya slice object dan slice notation menggunakan argumen yang sama. Hanya saja slice object lebih eksplisit sehingga lebih mudah dipahami.

Insialisasi slice object dilakukan seperti contoh pada kode berikut:

slice_object = slice(start, stop, step)
slice = l[slice_object]

Pada kode di atas start melambangkan awal indeks dimana pemotongan dimulai , stop untuk melambangkan indeks pembatas pemotongan, dan step melambangkan lompatan indeks. Pola ini sama persis dengan konsep pada penggunaan slice notation.

Kode di bawah ini menggunakan slice object, dan menghasilkan output yang sama dengan kode sebelumnya yang menggunakan slice notation.

Bacaan lebih lanjut mengenai bagaimana melakukan slicing terhadap nilai string dapat dilihat pada artikel: https://koding.alza.web.id/string-slicing/

Sekian dan terima kasih,

Salam

The answers above don't discuss slice assignment. To understand slice assignment, it's helpful to add another concept to the ASCII art:

                +---+---+---+---+---+---+
                | P | y | t | h | o | n |
                +---+---+---+---+---+---+
Slice position: 0   1   2   3   4   5   6
Index position:   0   1   2   3   4   5

>>> p = ['P','y','t','h','o','n']
# Why the two sets of numbers:
# indexing gives items, not lists
>>> p[0]
 'P'
>>> p[5]
 'n'

# Slicing gives lists
>>> p[0:1]
 ['P']
>>> p[0:2]
 ['P','y']

One heuristic is, for a slice from zero to n, think: "zero is the beginning, start at the beginning and take n items in a list".

>>> p[5] # the last of six items, indexed from zero
 'n'
>>> p[0:5] # does NOT include the last item!
 ['P','y','t','h','o']
>>> p[0:6] # not p[0:5]!!!
 ['P','y','t','h','o','n']

Another heuristic is, "for any slice, replace the start by zero, apply the previous heuristic to get the end of the list, then count the first number back up to chop items off the beginning"

>>> p[0:4] # Start at the beginning and count out 4 items
 ['P','y','t','h']
>>> p[1:4] # Take one item off the front
 ['y','t','h']
>>> p[2:4] # Take two items off the front
 ['t','h']
# etc.

The first rule of slice assignment is that since slicing returns a list, slice assignment requires a list (or other iterable):

>>> p[2:3]
 ['t']
>>> p[2:3] = ['T']
>>> p
 ['P','y','T','h','o','n']
>>> p[2:3] = 't'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only assign an iterable

The second rule of slice assignment, which you can also see above, is that whatever portion of the list is returned by slice indexing, that's the same portion that is changed by slice assignment:

>>> p[2:4]
 ['T','h']
>>> p[2:4] = ['t','r']
>>> p
 ['P','y','t','r','o','n']

The third rule of slice assignment is, the assigned list (iterable) doesn't have to have the same length; the indexed slice is simply sliced out and replaced en masse by whatever is being assigned:

>>> p = ['P','y','t','h','o','n'] # Start over
>>> p[2:4] = ['s','p','a','m']
>>> p
 ['P','y','s','p','a','m','o','n']

The trickiest part to get used to is assignment to empty slices. Using heuristic 1 and 2 it's easy to get your head around indexing an empty slice:

>>> p = ['P','y','t','h','o','n']
>>> p[0:4]
 ['P','y','t','h']
>>> p[1:4]
 ['y','t','h']
>>> p[2:4]
 ['t','h']
>>> p[3:4]
 ['h']
>>> p[4:4]
 []

And then once you've seen that, slice assignment to the empty slice makes sense too:

>>> p = ['P','y','t','h','o','n']
>>> p[2:4] = ['x','y'] # Assigned list is same length as slice
>>> p
 ['P','y','x','y','o','n'] # Result is same length
>>> p = ['P','y','t','h','o','n']
>>> p[3:4] = ['x','y'] # Assigned list is longer than slice
>>> p
 ['P','y','t','x','y','o','n'] # The result is longer
>>> p = ['P','y','t','h','o','n']
>>> p[4:4] = ['x','y']
>>> p
 ['P','y','t','h','x','y','o','n'] # The result is longer still

Note that, since we are not changing the second number of the slice (4), the inserted items always stack right up against the 'o', even when we're assigning to the empty slice. So the position for the empty slice assignment is the logical extension of the positions for the non-empty slice assignments.

Backing up a little bit, what happens when you keep going with our procession of counting up the slice beginning?

>>> p = ['P','y','t','h','o','n']
>>> p[0:4]
 ['P','y','t','h']
>>> p[1:4]
 ['y','t','h']
>>> p[2:4]
 ['t','h']
>>> p[3:4]
 ['h']
>>> p[4:4]
 []
>>> p[5:4]
 []
>>> p[6:4]
 []

With slicing, once you're done, you're done; it doesn't start slicing backwards. In Python you don't get negative strides unless you explicitly ask for them by using a negative number.

>>> p[5:3:-1]
 ['n','o']

There are some weird consequences to the "once you're done, you're done" rule:

>>> p[4:4]
 []
>>> p[5:4]
 []
>>> p[6:4]
 []
>>> p[6]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

In fact, compared to indexing, Python slicing is bizarrely error-proof:

>>> p[100:200]
 []
>>> p[int(2e99):int(1e99)]
 []

This can come in handy sometimes, but it can also lead to somewhat strange behavior:

>>> p
 ['P', 'y', 't', 'h', 'o', 'n']
>>> p[int(2e99):int(1e99)] = ['p','o','w','e','r']
>>> p
 ['P', 'y', 't', 'h', 'o', 'n', 'p', 'o', 'w', 'e', 'r']

Depending on your application, that might... or might not... be what you were hoping for there!


Below is the text of my original answer. It has been useful to many people, so I didn't want to delete it.

>>> r=[1,2,3,4]
>>> r[1:1]
[]
>>> r[1:1]=[9,8]
>>> r
[1, 9, 8, 2, 3, 4]
>>> r[1:1]=['blah']
>>> r
[1, 'blah', 9, 8, 2, 3, 4]

This may also clarify the difference between slicing and indexing.

Apa itu slicing di Python?

Slicing merupakan teknik memilih data dari sebuah set data. Misal kita memiliki data berat badan mahasiswa: 65, 78, 77, 100, 56. Maka jika kita urutkan maka urutan pertama adalah 65, urutan kedua adalah 78, urutan ketiga adalah 77, urutan keempat adalah 100, dan urutan terakhir adala 56.

Apa fungsi slicing?

Dengan melakukan slicing, gigi memiliki ruang untuk bergerak, sehingga gigi dapat bergeser dan menjadi lebih rapih posisinya. Selain itu, ada beberapa kasus dapat mengurangi gigi yang maju atau tonggos.