Mengiris dengan Python mendapatkan sub-string dari sebuah string. Kisaran pemotongan diatur sebagai parameter i. e. mulai, berhenti dan melangkah
Sintaksis
Mari kita lihat sintaksnya
# slicing from index start to index stop-1 arr[start:stop] # slicing from index start to the end arr[start:] # slicing from the beginning to index stop - 1 arr[:stop] # slicing from the index start to index stop, by skipping step arr[start:stop:step]Contoh Pemotongan
Dalam contohnya kita akan memotong string dari awal, akhir, dengan melompati langkah-langkah, dll
Keluaran
String = Hello! How are you? Slicing from index start to index stop-1 = ! H Slicing from index start to the end = lo! How are you? Slicing from the beginning to index stop - 1 = Hello Slicing from the index start to index stop, by skipping step = !Hw _Iris seutas tali
Mari kita mengiris string -
Keluaran
String = Hello! How are you? String after slicing = owIris string dengan langkah
Langkah ini digunakan untuk mengatur kenaikan antara setiap indeks untuk mengiris −
Keluaran
String = Hello! How are you? String after slicing = o rIris Tuple
Kita dapat mengiris bagian tuple −
Keluaran
Tuple = ('Tim', 'Mark', 'Harry', 'Anthony', 'Forrest', 'Alex', 'Rock') Tuple after slicing = ('Anthony', 'Forrest') _Iris Tuple dengan langkah
Kita dapat mengiris bagian tuple dan juga menggunakan parameter langkah untuk mengatur kenaikan antara setiap indeks untuk mengiris −
Keluaran
Tuple = ('Tim', 'Mark', 'Harry', 'Anthony', 'Forrest', 'Alex', 'Rock', 'Paul', 'David', 'Steve') Tuple after slicing = ('Harry', 'Forrest', 'Rock')Potong Daftar
Keluaran
List = ['p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] List after slicing = ['s', 't', 'u'] _Iris Daftar dengan langkah
Kita dapat mengiris bagian dari Daftar dan juga menggunakan parameter step untuk mengatur kenaikan antara setiap indeks untuk mengiris -
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 step0arr[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 step3
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 step4arr[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 step6arr[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 step7arr[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 step8arr[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
GEE0arr[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________7______7GEE4GEE5GEE6GEE5GEE8________7______99
EK0arr[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________7______7EK4GEE4GEE5EK4EK8GEE5EK4__________17_____
SEGOSE3SEGOSE4
SEGOSE5SEGOSE6
SEGOSE5arr[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
SEGOSE3SKEEGROFSKEEG0
SEGOSE3SKEEGROFSKEEG2
SEGOSE3SKEEGROFSKEEG4
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 stepContoh 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 step0arr[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
SEGOSE3KSFO3arr[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 step8KSFO5
Keluaran
GEEContoh 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 step0arr[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 demonstrate4GEE4# Python program to demonstrate6GEE6# Python program to demonstrate6GEE8KSFO5
Keluaran
EKContoh 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 step0arr[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 demonstrate4EK4GEE4________85______6List = ['p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] List after slicing = ['s', 't', 'u'] _27_______4________27______8# Python program to demonstrate6EK4GEE8________56______5