There are two string methods for this, find() and index(). The difference between the two is what happens when the search string isn't found. find() returns -1 and index() raises a ValueError.

Using find()

>>> myString = 'Position of a character'
>>> myString.find('s')
2
>>> myString.find('x')
-1

Using index()

>>> myString = 'Position of a character'
>>> myString.index('s')
2
>>> myString.index('x')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found

From the Python manual

string.find(s, sub[, start[, end]])
Return the lowest index in s where the substring sub is found such that sub is wholly contained in s[start:end]. Return -1 on failure. Defaults for start and end and interpretation of negative values is the same as for slices.

And:

string.index(s, sub[, start[, end]])
Like find() but raise ValueError when the substring is not found.

Answer from Eli Bendersky on Stack Overflow
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Extract a Substring from a String in Python (Position, Regex) | note.nkmk.me
April 29, 2025 - This article explains how to extract a substring from a string in Python. You can extract a substring by specifying its position and length, or by using regular expression (regex) patterns. Extract a ...
๐ŸŒ
IONOS
ionos.com โ€บ digital guide โ€บ websites โ€บ web development โ€บ python string index
How to find the position of a substring with Python string index
January 23, 2024 - Python string index lets you find the position of a substring in a string. Hereโ€™s how the method works and what it offers.
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ how-to-index-and-slice-strings-in-python-3
How To Index and Slice Strings in Python | DigitalOcean
September 29, 2025 - The substring โ€œarkโ€ is printed from the string โ€œSammy Shark!โ€ because the character โ€œaโ€ occurs at the -4 index number position, and the character โ€œkโ€ occurs before the -1 index number position.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ standard-library โ€บ str โ€บ find
Python str find() - Locate Substring | Vultr Docs
December 25, 2024 - Search for a substring that does not exist in the main string. Observe the returned result. ... main_string = "Hello, welcome to the world of Python." position = main_string.find("goodbye") print(position) Explain Code
๐ŸŒ
Real Python
realpython.com โ€บ lessons โ€บ index-python-substring
Use .index() to Get the Location of the Substring (Video) โ€“ Real Python
00:25 It is aptly named .index(). So you can say text_lower.index() and then pass it as an argument the substring, and then Python will return to you the index position of the first letter of the first occurrence of the substring in your string.
Published ย  April 4, 2023
Find elsewhere
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python โ€บ substrings
Python | Substrings | Codecademy
June 18, 2025 - In Python, substrings can be obtained by using the slicing feature on a string variable. A slice can be made in a specific position within the string, or it can be made at the default index.
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-substring-example
Substring in Python language
In the following example, we will take a string and get the substring that starts at position 6 and spans until position 12. mystring = 'pythonexamples.org' substring = mystring[6:12] print(substring)
๐ŸŒ
Built In
builtin.com โ€บ software-engineering-perspectives โ€บ python-substring-indexof
5 Ways to Find the Index of a Substring in Python | Built In
A string in Python can be sliced using the slice() method. You can specify which index value in the string to begin slicing, which index to end slicing and how many steps to slice at (if wanting to skip every other character, etc.). The result will return the characters in a string based on the slice specifications. Substrings can be fetched from a larger string in Python using the slice() method.
๐ŸŒ
Python Tutorial
pythontutorial.net โ€บ home โ€บ python string methods โ€บ python string index()
Python String index(): Locating the Index of a Substring in a String
December 28, 2020 - The following example shows how to use the index() method to find the substring 'Python' in the string 'Python will, Python will rock you.': s = 'Python will, Python will rock you.' position = s.index('Python') print(position)Code language: PHP (php)
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-string-index-method
Python String index() Method - GeeksforGeeks
May 2, 2025 - The index() method in Python is used to find the position of a specified substring within a given string. It is similar to the find() method but raises a ValueError if the substring is not found, while find() returns -1. This can be helpful ...
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-find-how-to-search-for-a-substring-in-a-string
Python find() โ€“ How to Search for a Substring in a String
July 25, 2022 - If the substring is present in the string, find() returns the index, or the character position, of the first occurrence of the specified substring from that given string.
๐ŸŒ
datagy
datagy.io โ€บ home โ€บ python posts โ€บ python strings โ€บ python: find an index (or all) of a substring in a string
Python: Find an Index (or all) of a Substring in a String โ€ข datagy
December 20, 2022 - When we apply the method to our string a_string, the result of that returns 0. This means that the substring begins at index position 0, of our string (i.e., itโ€™s the first word).
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Search for a String in Python (Check If a Substring Is Included/Get a Substring Position) | note.nkmk.me
May 7, 2023 - The in operator in Python (for list, string, dictionary, etc.) You can get the position of a given substring in the string with the find() method of str.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ find-the-index-of-a-substring-in-python
Find the Index of a Substring in Python - GeeksforGeeks
July 23, 2025 - In this article, we will explore some simple and commonly used methods to find the index of a substring in Python. The find() method searches for the first occurrence of the specified substring and returns its starting index.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ standard-library โ€บ str โ€บ index
Python str index() - Find Substring Index | Vultr Docs
November 26, 2024 - full_string = "Hello, welcome to Python." substring = "welcome" index_position = full_string.index(substring) print(index_position) Explain Code
๐ŸŒ
Python Central
pythoncentral.io โ€บ how-to-get-a-substring-from-a-string-in-python-slicing-strings
How to Get a Sub-string From a String in Python - Slicing Strings (Part 1) | Python Central
December 29, 2021 - So what we see here is a "sub-string". To get a sub-string from a string, it's as simple as inputting the desired start position of the string as well as the desired end position.
๐ŸŒ
Centron
centron.de โ€บ startseite โ€บ python string substring
Python String Substring โ€“ Searching and Extracting
March 10, 2025 - s = 'My Name is Pankaj' if 'Name' in s: print('Substring found') if s.find('Name') != -1: print('Substring found') Note that find() function returns the index position of the substring if itโ€™s found, otherwise it returns -1.