Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python Python string find() examples

Hilton D

Active Coder
I'm looking for examples, but I'm not finding any.
Are there any examples on the internet? I'd like to know what it returns when it can't find something, as well as how to specify the starting and ending points, which I assume will be 0, -1.
Python:
>>> x = "Hello World"
>>> x.find('World')
6
>>> x.find('Aloha');
-1
Could you please help me? But I'm not convinced.
 
Solution
from example string.find will return the starting index of the first occurrence.If no match is found it will return -1
If given a start and end index it will look for the substring starting at index_start and search to index_end.

Python:
string = 'in the ballpark'
x = string.find('ball')
print(f'''ball starts at index {x}.
index {x} is the letter {string[x]}.
remember indexes will start at 0.
this will be the letter {string[0]}.''')


string2 = 'This is another string about stringfind'
start = 5
end = len(string2)-1
x = string2.find('string', start, end)
print(f'''
This search started at index {start} which is the letter {string2[start]}
and ends with the index {end} which is the letter {string2[end]}.
The substring is found at index {x} which...
from example string.find will return the starting index of the first occurrence.If no match is found it will return -1
If given a start and end index it will look for the substring starting at index_start and search to index_end.

Python:
string = 'in the ballpark'
x = string.find('ball')
print(f'''ball starts at index {x}.
index {x} is the letter {string[x]}.
remember indexes will start at 0.
this will be the letter {string[0]}.''')


string2 = 'This is another string about stringfind'
start = 5
end = len(string2)-1
x = string2.find('string', start, end)
print(f'''
This search started at index {start} which is the letter {string2[start]}
and ends with the index {end} which is the letter {string2[end]}.
The substring is found at index {x} which is the letter {string2[x]}.
''')
print(f'''
We can also printing from the index using string slice such as string2[:x] will return
{string2[:x]}
or string2[x:] which will return
{string2[x:]}
''')

output
Code:
This search started at index 5 which is the letter i
and ends with the index 38 which is the letter d.
The substring is found at index 16 which is the letter s.


We can also printing from the index using string slice such as string2[:x] will return
This is another

or string2[x:] which will return
string about stringfind
 
Last edited:
Solution

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom