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)
2
starting index 5
start and end index (5, 8)
3

Keluaran

3
_

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

initial_strings :  xyze  
character_to_find :  b
No such character available in string xyze
2=
initial_strings :  xyze  
character_to_find :  b
No such character available in string xyze
4

 

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 xyze
7
initial_strings :  xyze  
character_to_find :  b
No such character available in string xyze
8
initial_strings :  xyze  
character_to_find :  b
No such character available in string xyze
9

 

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 xyze
7
initial_string :  abcdef 
character_to_find :  b
Character b is present at 2
3
initial_string :  abcdef 
character_to_find :  b
Character b is present at 2
4

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

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 2
6=
initial_string :  abcdef 
character_to_find :  b
Character b is present at 2
8

 

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 xyze
0=
initial_strings :  abcdef   xyze  
character_to_find :  b
Character b in string abcdef is present at 2
No such character available in string xyze
2

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

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

________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 xyze
9
initial_strings :  xybze
character_to_find :  b
3
0

 

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
3
4
initial_strings :  xybze
character_to_find :  b
3
5=
initial_strings :  xybze
character_to_find :  b
3
7

initial_strings :  xybze
character_to_find :  b
3
4
starting index 5
start and end index (5, 8)
2
initial_strings :  xyze  
character_to_find :  b
No such character available in string xyze
7string1string2string3
initial_strings :  xyze  
character_to_find :  b
No such character available in string xyze
7

string5string6string7string8string9 =0=1

=2 =3

initial_strings :  xybze
character_to_find :  b
3
4
starting index 5
start and end index (5, 8)
2
initial_strings :  xyze  
character_to_find :  b
No such character available in string xyze
7=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 xyze
0=
initial_strings :  abcdef   xyze  
character_to_find :  b
Character b in string abcdef is present at 2
No such character available in string xyze
2

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

starting index 5
start and end index (5, 8)
2
initial_strings :  xyze  
character_to_find :  b
No such character available in string xyze
7letter2letter3
initial_strings :  abcdef   xyze  
character_to_find :  b
Character b in string abcdef is present at 2
No such character available in string xyze
9
initial_strings :  xybze
character_to_find :  b
3
0

 

letter_6

________35

starting 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)
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 xyze
7
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
3
5=
starting index 5
start and end index (5, 8)
01string9 =0

starting 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
3
5== letter9
initial_strings :  xybze
character_to_find :  b
3
3

initial_strings :  xybze
character_to_find :  b
3
4
starting index 5
start and end index (5, 8)
2
initial_strings :  xyze  
character_to_find :  b
No such character available in string xyze
7
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
3
3

initial_strings :  xybze
character_to_find :  b
3
4
starting index 5
start and end index (5, 8)
2
initial_strings :  xyze  
character_to_find :  b
No such character available in string xyze
7
starting index 5
start and end index (5, 8)
39string2string3
starting index 5
start and end index (5, 8)
42string7
starting 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 2
8

 

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 xyze
0=
initial_strings :  abcdef   xyze  
character_to_find :  b
Character b in string abcdef is present at 2
No such character available in string xyze
2

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

starting index 5
start and end index (5, 8)
2
initial_strings :  xyze  
character_to_find :  b
No such character available in string xyze
7
initial_strings :  abcdef   xyze  
character_to_find :  b
Character b in string abcdef is present at 2
No such character available in string xyze
6letter3
starting 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 xyze
9
initial_strings :  xybze
character_to_find :  b
3
0

 

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=0
initial_strings :  xybze
character_to_find :  b
3
3

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

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

initial_strings :  xybze
character_to_find :  b
3
4
starting index 5
start and end index (5, 8)
2
initial_strings :  xyze  
character_to_find :  b
No such character available in string xyze
7string1string2string3
initial_strings :  xyze  
character_to_find :  b
No such character available in string xyze
7

starting index 5
start and end index (5, 8)
15
initial_strings :  xyze  
character_to_find :  b
No such character available in string xyze
00string7
initial_strings :  xyze  
character_to_find :  b
No such character available in string xyze
02string9 =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=0
initial_strings :  xybze
character_to_find :  b
3
3

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

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

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

initial_strings :  xybze
character_to_find :  b
3
4
starting index 5
start and end index (5, 8)
2
initial_strings :  xyze  
character_to_find :  b
No such character available in string xyze
7string1string2string3
initial_strings :  xyze  
character_to_find :  b
No such character available in string xyze
7

starting index 5
start and end index (5, 8)
15
initial_strings :  xyze  
character_to_find :  b
No such character available in string xyze
32string7
initial_strings :  xyze  
character_to_find :  b
No such character available in string xyze
34________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 xyze
39

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

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