Apa yang mengiris dalam pertanyaan wawancara python?

* 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 step
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 step
1
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
2

 

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 step
4
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
1
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
6
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
7
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
8
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
9

GEE
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 step
1
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
6________1______7
GEE
4
GEE
5
GEE
6
GEE
5
GEE
8
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
9

________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 step
1
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
6________1______7
EK
4
GEE
4
GEE
5
EK
4
EK
8
GEE
5
EK
4________1__1___9

 

SEGOSE
3
SEGOSE
4

SEGOSE
5
SEGOSE
6

SEGOSE
5
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
9

SEGOSE
3
SKEEGROFSKEEG
0

SEGOSE
3
SKEEGROFSKEEG
2

SEGOSE
3
SKEEGROFSKEEG
4

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 step
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 step
1
KSFO
0

 

KSFO
1

SEGOSE
3
KSFO
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 step
8
KSFO
5

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 step
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 step
1
KSFO
0

 

KSFO
1

SEGOSE
3# Python program to demonstrate4
GEE
4# Python program to demonstrate6________11______6# Python program to demonstrate6
GEE
8
KSFO
5

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 step
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 step
1
KSFO
0

 

KSFO
1

SEGOSE
3# Python program to demonstrate4
EK
4
GEE
4# Python program to demonstrate6
EK
4________21______8# Python program to demonstrate6
EK
4
GEE
8________50______5

Apa yang dimaksud dengan mengiris dengan Python?

Python slice[] Fungsi . 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. 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 yang dimaksud dengan mengiris dengan Python jelaskan dengan contoh string?

Python slicing adalah tentang mendapatkan sub-string dari string yang diberikan dengan mengirisnya masing-masing dari awal hingga akhir .

Apa itu irisan dan bagaimana cara kerjanya?

Slice adalah platform pinjaman digital yang menawarkan Kartu Kredit Slice bekerja sama dengan beberapa NBFC . Dengan kartu ini, Anda bisa mendapatkan cashback hingga 2% di setiap transaksi dalam bentuk reward.

Apa yang dimaksud dengan mengiris dan mengindeks dengan Python jelaskan dengan sebuah contoh?

Apa itu Pengindeksan dan Pemotongan? . Pengindeksan digunakan untuk mendapatkan elemen individu. Mengiris. Slicing digunakan untuk mendapatkan urutan elemen . Pengindeksan dan Pemotongan dapat dilakukan dalam jenis Urutan Python seperti daftar, string, tuple, objek jangkauan.

Bài mới nhất

Chủ Đề