Apa itu splicing dengan python?

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 = ow

Iris string dengan langkah

Langkah ini digunakan untuk mengatur kenaikan antara setiap indeks untuk mengiris −

Keluaran

String = Hello! How are you? String after slicing = o r

Iris 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 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 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

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 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

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 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

Apa artinya penyambungan Python?

Mengiris dengan Python adalah fitur yang memungkinkan pengaksesan bagian urutan seperti string, tupel, dan daftar . Anda juga dapat menggunakannya untuk mengubah atau menghapus item urutan yang dapat diubah seperti daftar. Slices juga dapat diterapkan pada objek pihak ketiga seperti array NumPy, serta seri Pandas dan bingkai data.

Bagaimana Anda menggunakan slicing dengan Python?

Python slice() Fungsi . Objek irisan digunakan untuk menentukan cara mengiris urutan. Anda dapat menentukan di mana memulai pemotongan, dan di mana harus mengakhiri. Anda juga dapat menentukan langkahnya, yang memungkinkan Anda untuk e. g. iris hanya setiap item lainnya. The slice() function returns a slice object. A slice object is used to specify how to slice a sequence. You can specify where to start the slicing, and where to end. You can also specify the step, which allows you to e.g. slice only every other item.

Apa itu mengiris dalam contoh Python?

Python slice() Fungsi . Objek irisan ini dapat digunakan untuk mendapatkan subbagian dari koleksi. Misalnya, jika kita ingin mendapatkan dua elemen pertama dari daftar sepuluh elemen? , di sini dapat digunakan potongan.

Apa itu mengiris string dengan Python?

Mengiris String . Specify the start index and the end index, separated by a colon, to return a part of the string.

Postingan terbaru

LIHAT SEMUA