Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Examples for string find() in Python

Status
Not open for further replies.

Soham1087

Newbie level 4
Newbie level 4
Joined
May 31, 2022
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Location
India
Activity points
96
I (beginner) am trying to find some examples but no luck.

Does anyone know of some examples on the net? I would like to know what it returns when it can't find, and how to specify from start to end, which I guess is going to be 0, -1.

From Scaler I got the following resolution, but still unsure about it. Need your valuable insights on it.

Python:
>>> x = "Hello World"
>>> x.find('World')
6
>>> x.find('Aloha');
-1
 

KlausST

Super Moderator
Staff member
Advanced Member level 7
Joined
Apr 17, 2014
Messages
23,525
Helped
4,761
Reputation
9,543
Reaction score
5,182
Trophy points
1,393
Activity points
156,068
Hi,

"-1" means "not found"
">=0" means the position in the string, beginning with 0

H --> pos 0
e --> pos 1
l --> pos 2
l --> pos 3
o --> pos 4
[ ] --> pos 5
W --> pos 6
o --> pos 7
...


Klaus
 

tepalia02

Advanced Member level 4
Full Member level 1
Joined
Jul 13, 2022
Messages
103
Helped
5
Reputation
10
Reaction score
18
Trophy points
18
Activity points
604
The example is correct. Start counting from 0. In "Hello world", the 0th character is 'H', 1st character is 'e' and similarly you can determine the positions of the rests. If you count from '0' you will see that the string 'World' starts from the 6th position. That's why you got the result 6 when you run
Code:
x.find('World')

Similarly you get -1 for your second code because 'aloha' is nowhere in the string 'Hello world'.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top