>>> x = "Hello World!"
>>> x[2:]
'llo World!'
>>> x[:2]
'He'
>>> x[:-2]
'Hello Worl'
>>> x[-2:]
'd!'
>>> x[2:-2]
'llo Worl'

Python calls this concept "slicing" and it works on more than just strings. Take a look here for a comprehensive introduction.

Answer from Paolo Bergantino on Stack Overflow
🌐
W3Schools
w3schools.com › python › python_strings.asp
Python Strings
Since strings are arrays, we can loop through the characters in a string, with a for loop. ... Learn more about For Loops in our Python For Loops chapter.
🌐
Codecademy
codecademy.com › docs › python › substrings
Python | Substrings | Codecademy
June 18, 2025 - A Python substring is a sequence of characters that are part of an original string. In Python, substrings can be obtained by using the slicing feature on a string variable.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-substring-a-string-in-python
How to Substring a String in Python - GeeksforGeeks
July 23, 2025 - Consider the string " GeeksForGeeks is best! ". Let's perform various string-slicing operations to extract various substrings · In this example, we are trying to extract the starting word from the string.we used string slicing to extract the substring "Geeks" from the beginning of the string.
🌐
Medium
medium.com › nerd-for-tech › python-substrings-everything-you-need-to-know-4cca526c07eb
Python Substrings: Everything You Need to Know | Nerd For Tech
July 21, 2021 - You can also define substring as a sequence of characters within a string. From the previous example, python substrings can be “Friday”, “at”, “meet”, and others.
🌐
Sentry
sentry.io › sentry answers › python › extract a substring from a string in python
Extract a substring from a string in Python
2 weeks ago - Extract substrings from Python strings using slice notation with [start:end] indexes, or use re.search() with regex patterns for matching specific formats
🌐
Real Python
realpython.com › python-string-contains-substring
How to Check if a Python String Contains a Substring – Real Python
December 1, 2024 - Note: If you want to check whether the substring is not in the string, then you can use not in: ... Because the substring "secret" is present in raw_file_content, the not in operator returns False. When you use in, the expression returns a Boolean value: ... In this code snippet, you use the membership operator to check whether "secret" is a substring of raw_file_content. If it is, then you’ll print a message to the terminal. Any indented code will only execute if the Python string that you’re checking contains the substring that you provide.
Find elsewhere
🌐
Python
docs.python.org › 3 › library › string.html
string — Common string operations
While other exceptions may still occur, this method is called “safe” because it always tries to return a usable string instead of raising an exception. In another sense, safe_substitute() may be anything other than safe, since it will silently ignore malformed templates containing dangling delimiters, unmatched braces, or placeholders that are not valid Python identifiers.
🌐
CodeSignal
codesignal.com › learn › courses › practicing-string-operations-and-type-conversions-in-python › lessons › exploring-substring-search-in-python-strings
Exploring Substring Search in Python Strings
We use the built-in zip() function to create pairs of original strings and substrings. We then use the find() method to find the first occurrence of each substring in its related original string. Python string objects have a built-in method called str.find(substring, starting_index=0), which comes in handy here.
🌐
Udemy
blog.udemy.com › home › it & development › software development › substrings in python and the many ways you can use them
Substrings in Python and Ways You Can Use Them - Udemy Blog
April 14, 2026 - This article will introduce you ... If a string is a sequence of characters, a Python substring is simply a sequence of characters within another string....
🌐
Built In
builtin.com › software-engineering-perspectives › python-substring-indexof
5 Ways to Find the Index of a Substring in Python | Built In
Below is a summary of what you need to remember about each Python string method. str.find(), str,index(): These return the lowest index of the substring.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-substring-a-string-in-python
How to Substring a String in Python
January 3, 2020 - Python offers many ways to substring a string. This is often called "slicing". Here is the syntax: string[start:end:step] Where, start: The starting index of the substring. The character at this index is included in the substring. If start is not in...
🌐
Python documentation
docs.python.org › 3 › library › stdtypes.html
Built-in Types — Python 3.14.6 documentation
The find() method should be used only if you need to know the position of sub. To check if sub is a substring or not, use the in operator: ... Perform a string formatting operation.
🌐
Codecademy
codecademy.com › article › how-to-check-if-a-string-contains-a-substring-in-python
How to Check if a String Contains a Substring in Python | Codecademy
In the next section, we’ll discover how to use the string .find() method to check for a substring in a string. The .find() method in Python is another built-in method that we can use to locate substrings within a string.
🌐
Python.org
discuss.python.org › python help
Use of pipe operator to search for substring in string - Python Help - Discussions on Python.org
June 2, 2022 - current scenario - 1) ('c' or 'a') in 'ab' gives, False as ('c' or 'a') evaluates to 'c' the substring check could be obtained, but need to write it using any builtin any([(i in 'ab') for i in ('c', 'a')]) gives, True in a match case statement, one could check for exact match of a string, x = 'ab' match x: case 'ab' | 'c': print(1) gives 1 but this pipe operator does not work for strings outside of match case statement. expected scenario - pipe operator works for strings ...
🌐
FROMDEV
fromdev.com › home › 2025 › april
How to Get a Substring of a String in Python: Slicing and Methods - FROMDEV
April 10, 2025 - Learn how to extract substrings in Python using slicing, find(), split(), and other string methods. Includes examples and best practices.
🌐
Frontendor
frontendor.com › blog › how-python-substring-a-string
How Python Substring A String
February 15, 2021 - In python substring is a sequence of characters within another string, In python, often they call it slicing a string.
🌐
MindStick
mindstick.com › forum › 161333 › how-do-you-get-a-substring-of-a-string-in-python
How do you get a substring of a string in Python? – MindStick
March 31, 2025 - Python allows users to extract substring portions from strings through slicing operations. Slicing permits you to obtain sections of strings by defining start and end positions like string[start:end], and start is included but end is omitted.