Python menemukan karakter pertama dalam string

Diberi string dan karakter, tugas Anda adalah menemukan posisi pertama karakter dalam string menggunakan Python. Jenis masalah ini adalah pemrograman yang sangat kompetitif di mana Anda perlu menemukan posisi karakter dalam sebuah string. Mari kita bahas beberapa metode untuk menyelesaikan masalah

Metode 1. Dapatkan posisi karakter di Python menggunakan rfind()

Metode Python String rfind() mengembalikan indeks tertinggi dari substring jika ditemukan dalam string yang diberikan. Jika tidak ditemukan maka mengembalikan -1

Python3




string= 'Geeks'

letter= starting index 5 start and end index (5, 8) 1

starting index 5 start and end index (5, 8) 2starting index 5 start and end index (5, 8) 3

Keluaran3 _

Metode 2. Dapatkan posisi karakter di Python menggunakan regex

ulang. metode search() mengembalikan Tidak Ada (jika polanya tidak cocok) atau re. MatchObject berisi informasi tentang bagian string yang cocok. Metode ini berhenti setelah pertandingan pertama,

Python3




starting index 5 start and end index (5, 8) 4 starting index 5 start and end index (5, 8) 5

string= starting index 5 start and end index (5, 8) 8

starting index 5 start and end index (5, 8) 9= initial_strings : xyze character_to_find : b No such character available in string xyze1

initial_strings : xyze character_to_find : b No such character available in string xyze2=initial_strings : xyze character_to_find : b No such character available in string xyze4

 

initial_strings : xyze character_to_find : b No such character available in string xyze_5

starting index 5 start and end index (5, 8) 2 initial_strings : xyze character_to_find : b No such character available in string xyze7initial_strings : xyze character_to_find : b No such character available in string xyze8initial_strings : xyze character_to_find : b No such character available in string xyze9

 

initial_string : abcdef character_to_find : b Character b is present at 2_0

starting index 5 start and end index (5, 8) 2 initial_strings : xyze character_to_find : b No such character available in string xyze7initial_string : abcdef character_to_find : b Character b is present at 23initial_string : abcdef character_to_find : b Character b is present at 24

Keluaranstarting index 5 start and end index (5, 8)

Metode 3. Dapatkan posisi karakter di Python menggunakan index()

Metode ini memunculkan Kesalahan Nilai jika karakter tidak ada

Python3




initial_string : abcdef character_to_find : b Character b is present at 2_5

initial_string : abcdef character_to_find : b Character b is present at 26= initial_string : abcdef character_to_find : b Character b is present at 28

 

initial_string : abcdef character_to_find : b Character b is present at 2_9

initial_strings : abcdef xyze character_to_find : b Character b in string abcdef is present at 2 No such character available in string xyze0= initial_strings : abcdef xyze character_to_find : b Character b in string abcdef is present at 2 No such character available in string xyze2

initial_strings : abcdef xyze character_to_find : b Character b in string abcdef is present at 2 No such character available in string xyze3

starting index 5 start and end index (5, 8) 2 initial_strings : xyze character_to_find : b No such character available in string xyze7initial_strings : abcdef xyze character_to_find : b Character b in string abcdef is present at 2 No such character available in string xyze6initial_strings : abcdef xyze character_to_find : b Character b in string abcdef is present at 2 No such character available in string xyze7

________26______8initial_strings : abcdef xyze character_to_find : b Character b in string abcdef is present at 2 No such character available in string xyze9initial_strings : xybze character_to_find : b 30

 

initial_strings : xybze character_to_find : b 3_1

________35starting index 5 start and end index (5, 8) _35_______3

initial_strings : xybze character_to_find : b 34initial_strings : xybze character_to_find : b 35= initial_strings : xybze character_to_find : b 37

initial_strings : xybze character_to_find : b 34starting index 5 start and end index (5, 8) 2 initial_strings : xyze character_to_find : b No such character available in string xyze7string1string2string3initial_strings : xyze character_to_find : b No such character available in string xyze7

string5string6string7string8string9 =0=1

=2 =3

initial_strings : xybze character_to_find : b 34starting index 5 start and end index (5, 8) 2 initial_strings : xyze character_to_find : b No such character available in string xyze7=7string2string3'Geeks'0

Keluaran

initial_strings : xyze character_to_find : b No such character available in string xyze_

Metode 4. Dapatkan posisi karakter di Python menggunakan loop

Dalam contoh ini, kita akan menggunakan perulangan Python untuk menemukan posisi karakter dalam string tertentu

Python3




initial_string : abcdef character_to_find : b Character b is present at 2_5

'Geeks'2= 'Geeks'4

 

initial_string : abcdef character_to_find : b Character b is present at 2_9

initial_strings : abcdef xyze character_to_find : b Character b in string abcdef is present at 2 No such character available in string xyze0= initial_strings : abcdef xyze character_to_find : b Character b in string abcdef is present at 2 No such character available in string xyze2

initial_strings : abcdef xyze character_to_find : b Character b in string abcdef is present at 2 No such character available in string xyze3

starting index 5 start and end index (5, 8) 2initial_strings : xyze character_to_find : b No such character available in string xyze7letter2letter3initial_strings : abcdef xyze character_to_find : b Character b in string abcdef is present at 2 No such character available in string xyze9initial_strings : xybze character_to_find : b 30

 

letter_6

________35starting index 5 start and end index (5, 8) _177_______ letter9

starting index 5 start and end index (5, 8) 00 starting index 5 start and end index (5, 8) 01starting index 5 start and end index (5, 8) 02 starting index 5 start and end index (5, 8) 03initial_strings : xyze character_to_find : b No such character available in string xyze7starting index 5 start and end index (5, 8) 05starting index 5 start and end index (5, 8) 06starting index 5 start and end index (5, 8) 07starting index 5 start and end index (5, 8) 08

________35______4starting index 5 start and end index (5, 8) 10 starting index 5 start and end index (5, 8) 11== starting index 5 start and end index (5, 8) 14

starting index 5 start and end index (5, 8) 15initial_strings : xybze character_to_find : b 35= starting index 5 start and end index (5, 8) 01string9 =0

starting index 5 start and end index (5, 8) 15starting index 5 start and end index (5, 8) 22

 

starting index 5 start and end index (5, 8) 10 initial_strings : xybze character_to_find : b 35== letter9initial_strings : xybze character_to_find : b 33

initial_strings : xybze character_to_find : b 34starting index 5 start and end index (5, 8) 2initial_strings : xyze character_to_find : b No such character available in string xyze7starting index 5 start and end index (5, 8) 32starting index 5 start and end index (5, 8) 33

starting index 5 start and end index (5, 8) 34initial_strings : xybze character_to_find : b 33

initial_strings : xybze character_to_find : b 34starting index 5 start and end index (5, 8) 2initial_strings : xyze character_to_find : b No such character available in string xyze7starting index 5 start and end index (5, 8) 39string2string3starting index 5 start and end index (5, 8) 42string7starting index 5 start and end index (5, 8) 44

Keluaran.  

initial_string : abcdef character_to_find : b Character b is present at 2

Metode 5. Dapatkan posisi karakter dalam Python menggunakan str. menemukan

Metode ini mengembalikan -1 jika karakter tidak ada.  

Python3




initial_string : abcdef character_to_find : b Character b is present at 2_5

'Geeks'2= 'Geeks'4

starting index 5 start and end index (5, 8) 49= initial_string : abcdef character_to_find : b Character b is present at 28

 

initial_string : abcdef character_to_find : b Character b is present at 2_9

initial_strings : abcdef xyze character_to_find : b Character b in string abcdef is present at 2 No such character available in string xyze0= initial_strings : abcdef xyze character_to_find : b Character b in string abcdef is present at 2 No such character available in string xyze2

initial_strings : abcdef xyze character_to_find : b Character b in string abcdef is present at 2 No such character available in string xyze3

starting index 5 start and end index (5, 8) 2initial_strings : xyze character_to_find : b No such character available in string xyze7initial_strings : abcdef xyze character_to_find : b Character b in string abcdef is present at 2 No such character available in string xyze6letter3starting index 5 start and end index (5, 8) 61starting index 5 start and end index (5, 8) 06

starting index 5 start and end index (5, 8) 63starting index 5 start and end index (5, 8) 64initial_strings : abcdef xyze character_to_find : b Character b in string abcdef is present at 2 No such character available in string xyze9initial_strings : xybze character_to_find : b 30

 

starting index 5 start and end index (5, 8) _67

starting index 5 start and end index (5, 8) 68= starting index 5 start and end index (5, 8) 70

starting index 5 start and end index (5, 8) 71= starting index 5 start and end index (5, 8) 73

 

starting index 5 start and end index (5, 8) 10 starting index 5 start and end index (5, 8) 68== starting index 5 start and end index (5, 8) 78=0initial_strings : xybze character_to_find : b 33

initial_strings : xybze character_to_find : b 34starting index 5 start and end index (5, 8) 2initial_strings : xyze character_to_find : b No such character available in string xyze7=7string2string3initial_strings : xyze character_to_find : b No such character available in string xyze7

starting index 5 start and end index (5, 8) 15starting index 5 start and end index (5, 8) 89

starting index 5 start and end index (5, 8) 34initial_strings : xybze character_to_find : b 33

initial_strings : xybze character_to_find : b 34starting index 5 start and end index (5, 8) 2initial_strings : xyze character_to_find : b No such character available in string xyze7string1string2string3initial_strings : xyze character_to_find : b No such character available in string xyze7

starting index 5 start and end index (5, 8) 15initial_strings : xyze character_to_find : b No such character available in string xyze00string7initial_strings : xyze character_to_find : b No such character available in string xyze02string9 =0=1

 

starting index 5 start and end index (5, 8) 10 starting index 5 start and end index (5, 8) 71== starting index 5 start and end index (5, 8) 78=0initial_strings : xybze character_to_find : b 33

initial_strings : xybze character_to_find : b 34starting index 5 start and end index (5, 8) 2initial_strings : xyze character_to_find : b No such character available in string xyze7=7string2string3initial_strings : xyze character_to_find : b No such character available in string xyze7

starting index 5 start and end index (5, 8) 15initial_strings : xyze character_to_find : b No such character available in string xyze21

starting index 5 start and end index (5, 8) 34initial_strings : xybze character_to_find : b 33

initial_strings : xybze character_to_find : b 34starting index 5 start and end index (5, 8) 2initial_strings : xyze character_to_find : b No such character available in string xyze7string1string2string3initial_strings : xyze character_to_find : b No such character available in string xyze7

starting index 5 start and end index (5, 8) 15initial_strings : xyze character_to_find : b No such character available in string xyze32string7initial_strings : xyze character_to_find : b No such character available in string xyze34________176______9 =0=1

Keluaran

initial_strings : abcdef xyze character_to_find : b Character b in string abcdef is present at 2 No such character available in string xyze

Metode 6. Dapatkan posisi karakter dalam Python menggunakan pemahaman daftar dan fungsi selanjutnya

Metode ini melibatkan penggunaan pemahaman daftar untuk membuat daftar tupel yang berisi indeks dan karakter untuk setiap karakter dalam string. Kita kemudian dapat menggunakan fungsi next untuk mengembalikan tuple pertama yang karakternya cocok dengan yang kita cari. Jika karakter tidak ditemukan, kami memunculkan ValueError

Python3




initial_strings : xyze character_to_find : b No such character available in string xyze_38 initial_strings : xyze character_to_find : b No such character available in string xyze39

________35starting index 5 start and end index (5, 8) _8_______41

________35starting index 5 start and end index (5, 8) _8_______43

________35starting index 5 start and end index (5, 8) _35_______2initial_strings : xybze character_to_find : b 33

starting index 5 start and end index (5, 8) 63initial_strings : xyze character_to_find : b No such character available in string xyze48 =0string9 initial_strings : xyze character_to_find : b No such character available in string xyze51initial_strings : xyze character_to_find : b No such character available in string xyze___________________________________________________________________________________________________________________________________________________________________________________________________________________________g

Postingan terbaru

LIHAT SEMUA