>>> 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
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
🌐
Codecademy
codecademy.com › docs › python › substrings
Python | Substrings | Codecademy
June 18, 2025 - A substring in Python is a contiguous sequence of characters extracted from an original string.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-substring-a-string-in-python
How to Substring a String in Python - GeeksforGeeks
July 23, 2025 - A String is a collection of characters arranged in a particular order. A portion of a string is known as a substring. For instance, suppose we have the string "GeeksForGeeks". In that case, some of its substrings are "Geeks", "For", "eeks", ...
🌐
Real Python
realpython.com › python-string-contains-substring
How to Check if a Python String Contains a Substring – Real Python
December 1, 2024 - The in operator is intuitive and readable, making it a straightforward way to evaluate substring existence. By the end of this tutorial, you’ll understand that: The in membership operator is the recommended way to check if a Python string contains a substring.
🌐
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
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.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › check-if-string-contains-substring-in-python
Check if String Contains Substring in Python - GeeksforGeeks
December 20, 2025 - The in operator in Python checks if one string occurs within another. It evaluates to True if the substring is present in the main string, otherwise False.
🌐
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...
🌐
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.
🌐
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 - 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. Python string extraction achieves high efficiency through this technique without needing memory allocation, thus making it the standard approach to extract substrings.
🌐
LearnPython.com
learnpython.com › blog › substring-of-string-python
How to Get a Substring of a String in Python | LearnPython.com
April 26, 2022 - You can break this string up into substrings, each of which has the str data type. Even if your string is a number, it is still of this data type. You can test this with the built-in type() function. Numbers may be of other types as well, including the decimal data type, which we discuss here. Much like arrays and lists in Python, strings can be sliced by specifying the start and the end indexes, inside square brackets and separated by a colon.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-string-find
Python String find() Method - GeeksforGeeks
April 26, 2025 - find() method in Python returns the index of the first occurrence of a substring within a given string. If the substring is not found, it returns -1. This method is case-sensitive, which means "abc" is treated differently from "ABC".
🌐
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 - We can extract a substring from a string using Python’s slice notation. The syntax is as follows: ... The variable substring will include all characters in the string, starting at the start index up to but not including the end index.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-get-all-substrings-of-given-string
Python - Get all substrings of given string - GeeksforGeeks
July 12, 2025 - A substring is any contiguous sequence of characters within the string. We'll discuss various methods to extract this substring from a given string by using a simple approach. List comprehension offers a concise way to create lists by applying ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-all-occurrences-of-substring-in-string
Python - All occurrences of substring in string - GeeksforGeeks
July 12, 2025 - Interview Questions · Examples · Quizzes · DSA Python · Data Science · NumPy · Pandas · Practice · Django · Flask · Last Updated : 12 Jul, 2025 · A substring is a contiguous occurrence of characters within a string.
🌐
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
The .index() method is an inbuilt method in Python that allows us to search for a substring within a string. If the substring exists, the method returns the position or index of the first occurrence of the substring.
🌐
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.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Extract a Substring from a String in Python (Position, Regex) | note.nkmk.me
April 29, 2025 - Since backslashes \ are used in special sequences like \d, it is convenient to use raw string notation by prefixing the string with r. ... If a match is found, re.search() returns a match object. You can retrieve the matched substring using the group() method of the match object.
🌐
Great Learning
mygreatlearning.com › blog › it/software development › what is a python substring?
What is a Python Substring and How to Create One
June 11, 2025 - A Python substring is a part of a larger string. Learn how to extract substrings and explore useful string methods for working with them.