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 Show
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
starting index 5 start and end index (5, 8)1 starting index 5 start and end index (5, 8)2 starting index 5 start and end index (5, 8)3 Keluaran 3_ Metode 2. Dapatkan posisi karakter di Python menggunakan regexulang. 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, Python3starting index 5 start and end index (5, 8)4 starting index 5 start and end index (5, 8)5
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 xyze7 initial_strings : xyze character_to_find : b No such character available in string xyze8 initial_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 xyze7 initial_string : abcdef character_to_find : b Character b is present at 23 initial_string : abcdef character_to_find : b Character b is present at 24 Keluaran starting 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 Python3initial_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 xyze7 initial_strings : abcdef xyze character_to_find : b Character b in string abcdef is present at 2 No such character available in string xyze6 initial_strings : abcdef xyze character_to_find : b Character b in string abcdef is present at 2 No such character available in string xyze7 ________26______8 initial_strings : abcdef xyze character_to_find : b Character b in string abcdef is present at 2 No such character available in string xyze9 initial_strings : xybze character_to_find : b 30
initial_strings : xybze character_to_find : b 3_1 ________35 starting index 5 start and end index (5, 8)_35_______3 initial_strings : xybze character_to_find : b 34 initial_strings : xybze character_to_find : b 35 = initial_strings : xybze character_to_find : b 37 initial_strings : xybze character_to_find : b 34 starting index 5 start and end index (5, 8)2 initial_strings : xyze character_to_find : b No such character available in string xyze7 string 1string 2string 3initial_strings : xyze character_to_find : b No such character available in string xyze7
initial_strings : xybze character_to_find : b 34 starting index 5 start and end index (5, 8)2 initial_strings : xyze character_to_find : b No such character available in string xyze7 = 7string 2string 3'Geeks' 0Keluaran initial_strings : xyze character_to_find : b No such character available in string xyze_ Metode 4. Dapatkan posisi karakter di Python menggunakan loopDalam contoh ini, kita akan menggunakan perulangan Python untuk menemukan posisi karakter dalam string tertentu Python3initial_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 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 xyze7 letter 2letter 3initial_strings : abcdef xyze character_to_find : b Character b in string abcdef is present at 2 No such character available in string xyze9 initial_strings : xybze character_to_find : b 30
________35 starting index 5 start and end index (5, 8)_177_______ letter 9starting index 5 start and end index (5, 8)00 starting index 5 start and end index (5, 8)01 starting index 5 start and end index (5, 8)02 starting index 5 start and end index (5, 8)03 initial_strings : xyze character_to_find : b No such character available in string xyze7 starting index 5 start and end index (5, 8)05 starting index 5 start and end index (5, 8)06 starting index 5 start and end index (5, 8)07 starting index 5 start and end index (5, 8)08 ________35______4 starting 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)15 initial_strings : xybze character_to_find : b 35 = starting index 5 start and end index (5, 8)01 string 9 = 0starting index 5 start and end index (5, 8)15 starting 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 = = letter 9initial_strings : xybze character_to_find : b 33 initial_strings : xybze character_to_find : b 34 starting index 5 start and end index (5, 8)2 initial_strings : xyze character_to_find : b No such character available in string xyze7 starting index 5 start and end index (5, 8)32 starting index 5 start and end index (5, 8)33 starting index 5 start and end index (5, 8)34 initial_strings : xybze character_to_find : b 33 initial_strings : xybze character_to_find : b 34 starting index 5 start and end index (5, 8)2 initial_strings : xyze character_to_find : b No such character available in string xyze7 starting index 5 start and end index (5, 8)39 string 2string 3starting index 5 start and end index (5, 8)42 string 7starting 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. menemukanMetode ini mengembalikan -1 jika karakter tidak ada. Python3initial_string : abcdef character_to_find : b Character b is present at 2_5
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)2 initial_strings : xyze character_to_find : b No such character available in string xyze7 initial_strings : abcdef xyze character_to_find : b Character b in string abcdef is present at 2 No such character available in string xyze6 letter 3starting index 5 start and end index (5, 8)61 starting index 5 start and end index (5, 8)06 starting index 5 start and end index (5, 8)63 starting index 5 start and end index (5, 8)64 initial_strings : abcdef xyze character_to_find : b Character b in string abcdef is present at 2 No such character available in string xyze9 initial_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 34 starting index 5 start and end index (5, 8)2 initial_strings : xyze character_to_find : b No such character available in string xyze7 = 7string 2string 3initial_strings : xyze character_to_find : b No such character available in string xyze7 starting index 5 start and end index (5, 8)15 starting index 5 start and end index (5, 8)89 starting index 5 start and end index (5, 8)34 initial_strings : xybze character_to_find : b 33 initial_strings : xybze character_to_find : b 34 starting index 5 start and end index (5, 8)2 initial_strings : xyze character_to_find : b No such character available in string xyze7 string 1string 2string 3initial_strings : xyze character_to_find : b No such character available in string xyze7 starting index 5 start and end index (5, 8)15 initial_strings : xyze character_to_find : b No such character available in string xyze00 string 7initial_strings : xyze character_to_find : b No such character available in string xyze02 string 9 = 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 34 starting index 5 start and end index (5, 8)2 initial_strings : xyze character_to_find : b No such character available in string xyze7 = 7string 2string 3initial_strings : xyze character_to_find : b No such character available in string xyze7 starting index 5 start and end index (5, 8)15 initial_strings : xyze character_to_find : b No such character available in string xyze21 starting index 5 start and end index (5, 8)34 initial_strings : xybze character_to_find : b 33 initial_strings : xybze character_to_find : b 34 starting index 5 start and end index (5, 8)2 initial_strings : xyze character_to_find : b No such character available in string xyze7 string 1string 2string 3initial_strings : xyze character_to_find : b No such character available in string xyze7 starting index 5 start and end index (5, 8)15 initial_strings : xyze character_to_find : b No such character available in string xyze32 string 7initial_strings : xyze character_to_find : b No such character available in string xyze34________176______9 = 0= 1Keluaran 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 selanjutnyaMetode 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 Python3initial_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 ________35 starting index 5 start and end index (5, 8)_8_______41 ________35 starting index 5 start and end index (5, 8)_8_______43 ________35 starting index 5 start and end index (5, 8)_35_______2 initial_strings : xybze character_to_find : b 33 starting index 5 start and end index (5, 8)63 initial_strings : xyze character_to_find : b No such character available in string xyze48 = 0string 9 initial_strings : xyze character_to_find : b No such character available in string xyze51 initial_strings : xyze character_to_find : b No such character available in string xyze___________________________________________________________________________________________________________________________________________________________________________________________________________________________g |