by · Published 20 March 2019 · Updated 20 March 2019 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 :
Untuk lebih mudah memahami pola tersebut, coba pelajari dan jalankan kode di bawah ini. Menggunakan slice objectAlternatif 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:
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".
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"
The first rule of slice assignment is that since slicing returns a list, slice assignment requires a list (or other 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:
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:
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:
And then once you've seen that, slice assignment to the empty slice makes sense too:
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?
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.
There are some weird consequences to the "once you're done, you're done" rule:
In fact, compared to indexing, Python slicing is bizarrely error-proof:
This can come in handy sometimes, but it can also lead to somewhat strange behavior:
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.
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.
|