* Seperti namanya, 'mengiris' mengambil bagian
* Sintaks untuk mengiris adalah [mulai. berhenti. melangkah]
* start adalah indeks awal dari mana mengiris daftar atau tupel
* stop adalah indeks akhir atau tempat sop
* langkah adalah jumlah langkah untuk melompat
* Nilai default untuk mulai adalah 0, berhenti adalah jumlah item, langkah adalah 1
* Slicing dapat dilakukan pada string, array, list, dan tuple
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print[numbers[1 : : 2]] #output : [2, 4, 6, 8, 10]
Pemotongan Python adalah tentang mendapatkan sub-string dari string yang diberikan dengan mengirisnya masing-masing dari awal hingga akhir.
Cara kerja pemotongan String dengan Python
Untuk memahami slicing kita akan menggunakan metode yang berbeda, disini kita akan membahas 2 metode slicing string, yang satu menggunakan metode in-build slice[] dan yang lainnya menggunakan [. ] potongan larik. Pemotongan string dengan Python adalah tentang mendapatkan sub-string dari string yang diberikan dengan mengirisnya masing-masing dari awal hingga akhir.
Mengiris python dapat dilakukan dengan dua cara
- Menggunakan metode slice[]
- Menggunakan pemotongan array [. . ] metode
Pelacak indeks untuk indeks positif dan negatif. Pengindeksan string dan pemotongan dengan python. Di sini, Negatif menjadi pertimbangan saat melacak string secara terbalik.
Metode 1. Menggunakan metode slice[]
Konstruktor slice[] membuat objek slice yang mewakili kumpulan indeks yang ditentukan oleh range[start, stop, step]
Sintaksis
- irisan [berhenti]
- irisan [mulai, berhenti, langkah]
Parameter. Mulailah. Mulai indeks tempat pemotongan objek dimulai. berhenti. Mengakhiri indeks tempat pemotongan objek berhenti. melangkah. Ini adalah argumen opsional yang menentukan kenaikan antara setiap indeks untuk pemotongan. Jenis Pengembalian. Mengembalikan objek irisan yang mengandung elemen dalam rentang yang diberikan saja.
Contoh
Python3
# Python program to demonstrate
# string slicing
# String slicing
arr[start:stop] # items start through stop-1 arr[start:] # items start through the rest of the array arr[:stop] # items from the beginning through stop-1 arr[:] # a copy of the whole array arr[start:stop:step] # start through not past stop, by step0
arr[start:stop] # items start through stop-1 arr[start:] # items start through the rest of the array arr[:stop] # items from the beginning through stop-1 arr[:] # a copy of the whole array arr[start:stop:step] # start through not past stop, by step1
arr[start:stop] # items start through stop-1 arr[start:] # items start through the rest of the array arr[:stop] # items from the beginning through stop-1 arr[:] # a copy of the whole array arr[start:stop:step] # start through not past stop, by step2
arr[start:stop] # items start through stop-1 arr[start:] # items start through the rest of the array arr[:stop] # items from the beginning through stop-1 arr[:] # a copy of the whole array arr[start:stop:step] # start through not past stop, by step_3
arr[start:stop] # items start through stop-1 arr[start:] # items start through the rest of the array arr[:stop] # items from the beginning through stop-1 arr[:] # a copy of the whole array arr[start:stop:step] # start through not past stop, by step4
arr[start:stop] # items start through stop-1 arr[start:] # items start through the rest of the array arr[:stop] # items from the beginning through stop-1 arr[:] # a copy of the whole array arr[start:stop:step] # start through not past stop, by step1
arr[start:stop] # items start through stop-1 arr[start:] # items start through the rest of the array arr[:stop] # items from the beginning through stop-1 arr[:] # a copy of the whole array arr[start:stop:step] # start through not past stop, by step6
arr[start:stop] # items start through stop-1 arr[start:] # items start through the rest of the array arr[:stop] # items from the beginning through stop-1 arr[:] # a copy of the whole array arr[start:stop:step] # start through not past stop, by step7
arr[start:stop] # items start through stop-1 arr[start:] # items start through the rest of the array arr[:stop] # items from the beginning through stop-1 arr[:] # a copy of the whole array arr[start:stop:step] # start through not past stop, by step8
arr[start:stop] # items start through stop-1 arr[start:] # items start through the rest of the array arr[:stop] # items from the beginning through stop-1 arr[:] # a copy of the whole array arr[start:stop:step] # start through not past stop, by step9
GEE0
arr[start:stop] # items start through stop-1 arr[start:] # items start through the rest of the array arr[:stop] # items from the beginning through stop-1 arr[:] # a copy of the whole array arr[start:stop:step] # start through not past stop, by step1
arr[start:stop] # items start through stop-1 arr[start:] # items start through the rest of the array arr[:stop] # items from the beginning through stop-1 arr[:] # a copy of the whole array arr[start:stop:step] # start through not past stop, by step6________1______7
GEE4
GEE5
GEE6
GEE5
GEE8
arr[start:stop] # items start through stop-1 arr[start:] # items start through the rest of the array arr[:stop] # items from the beginning through stop-1 arr[:] # a copy of the whole array arr[start:stop:step] # start through not past stop, by step9
________21______0
arr[start:stop] # items start through stop-1 arr[start:] # items start through the rest of the array arr[:stop] # items from the beginning through stop-1 arr[:] # a copy of the whole array arr[start:stop:step] # start through not past stop, by step1
arr[start:stop] # items start through stop-1 arr[start:] # items start through the rest of the array arr[:stop] # items from the beginning through stop-1 arr[:] # a copy of the whole array arr[start:stop:step] # start through not past stop, by step6________1______7
EK4
GEE4
GEE5
EK4
EK8
GEE5
EK4________1__1___9
SEGOSE3
SEGOSE4
SEGOSE5
SEGOSE6
SEGOSE5
arr[start:stop] # items start through stop-1 arr[start:] # items start through the rest of the array arr[:stop] # items from the beginning through stop-1 arr[:] # a copy of the whole array arr[start:stop:step] # start through not past stop, by step9
SEGOSE3
SKEEGROFSKEEG0
SEGOSE3
SKEEGROFSKEEG2
SEGOSE3
SKEEGROFSKEEG4
Keluaran.
String slicing AST SR GITA
Metode 2. Menggunakan pemotongan Daftar/array [. . ] metode
Dalam Python, sintaks pengindeksan dapat digunakan sebagai pengganti objek slice. Ini adalah cara yang mudah dan nyaman untuk mengiris string menggunakan pengirisan daftar dan pengirisan Array baik dari segi sintaksis maupun dari segi eksekusi. Awal, akhir, dan langkah memiliki mekanisme yang sama dengan konstruktor slice[].
Di bawah ini kita akan melihat pemotongan string dengan Python dengan contoh
Sintaksis
arr[start:stop] # items start through stop-1 arr[start:] # items start through the rest of the array arr[:stop] # items from the beginning through stop-1 arr[:] # a copy of the whole array arr[start:stop:step] # start through not past stop, by step_
Contoh 1
Dalam contoh ini, kita akan melihat mengiris daftar python indeks mulai dari 0 indeks dan diakhiri dengan indeks 2 [berhenti di 3-1=2 ]
Python3
# Python program to demonstrate
# string slicing
# String slicing
arr[start:stop] # items start through stop-1 arr[start:] # items start through the rest of the array arr[:stop] # items from the beginning through stop-1 arr[:] # a copy of the whole array arr[start:stop:step] # start through not past stop, by step0
arr[start:stop] # items start through stop-1 arr[start:] # items start through the rest of the array arr[:stop] # items from the beginning through stop-1 arr[:] # a copy of the whole array arr[start:stop:step] # start through not past stop, by step1
KSFO0
KSFO1
SEGOSE3
KSFO3
arr[start:stop] # items start through stop-1 arr[start:] # items start through the rest of the array arr[:stop] # items from the beginning through stop-1 arr[:] # a copy of the whole array arr[start:stop:step] # start through not past stop, by step8
KSFO5
Keluaran
GEE
Contoh 2
Dalam contoh ini, kita akan melihat contoh mulai dari indeks 1 dan diakhiri dengan indeks 5 [berhenti di 3-1=2 ], dan langkah melompatinya adalah 2. Ini adalah contoh yang bagus dari Python mengiris string demi karakter
Python3
# Python program to demonstrate
# string slicing
# String slicing
arr[start:stop] # items start through stop-1 arr[start:] # items start through the rest of the array arr[:stop] # items from the beginning through stop-1 arr[:] # a copy of the whole array arr[start:stop:step] # start through not past stop, by step0
arr[start:stop] # items start through stop-1 arr[start:] # items start through the rest of the array arr[:stop] # items from the beginning through stop-1 arr[:] # a copy of the whole array arr[start:stop:step] # start through not past stop, by step1
KSFO0
KSFO1
SEGOSE3
# Python program to demonstrate
4GEE4
# Python program to demonstrate
6________11______6# Python program to demonstrate
6GEE8
KSFO5
Keluaran
EK
Contoh 3
Dalam contoh ini, kita akan melihat contoh mulai dari -1 indeks dan diakhiri dengan -12 indeks [berhenti di 3-1=2 ] dan langkah melompatinya adalah -2
Python3
# Python program to demonstrate
# string slicing
# String slicing
arr[start:stop] # items start through stop-1 arr[start:] # items start through the rest of the array arr[:stop] # items from the beginning through stop-1 arr[:] # a copy of the whole array arr[start:stop:step] # start through not past stop, by step0
arr[start:stop] # items start through stop-1 arr[start:] # items start through the rest of the array arr[:stop] # items from the beginning through stop-1 arr[:] # a copy of the whole array arr[start:stop:step] # start through not past stop, by step1
KSFO0
KSFO1
SEGOSE3
# Python program to demonstrate
4EK4
GEE4
# Python program to demonstrate
6EK4________21______8
# Python program to demonstrate
6EK4
GEE8________50______5