>>> 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
🌐
IONOS
ionos.com › digital guide › websites › web development › python substring
How to create and edit Python substrings - IONOS
July 19, 2023 - If you want to extract sub­strings starting from the end of a string, you can use negative indices. s = "Python is a popular language." result = s[-8:]pythonl · The “result” variable in the example above extracts the last 8 letters from ...
🌐
Python Examples
pythonexamples.org › python-substring-example
Substring in Python language
The string mystring[-15:-5] slices the string mystring starting from the 15th character from the end (m) and ending at the 5th character from the end (r), resulting in the substring 'examples. The print function then outputs this substring. ... If we do not provide any start or end position, ...
🌐
LearnDataSci
learndatasci.com › solutions › python-substring
Quickly Substring a String in Python – LearnDataSci
As the output shows, our substring starts at index 8 of example_string, which is 's'. The slice then returns the rest of the string, giving us the substring 'string'. ... Similarly to [start:], we can use [:end], which only requires you to enter an end point with the parameter end.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-substring-a-string-in-python
How to Substring a String in Python
January 3, 2020 - 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.
🌐
IT Explore
itexplore.org › homepage › tips › retrieve a substring from the end in python
Retrieve a substring from the end in Python | IT Explore
February 26, 2025 - """ if string is None: return "" return string[-length:] # Example 1: When string is None string = None length = 3 substring = substring_from_end(string, length) print(substring) # Output: "" # Example 2: Extracting the last 3 characters from ...
🌐
Python Examples
pythonexamples.org › python-substring-from-specific-index-to-end
Substring from specific Index to End
To find the substring of a given string from specific index to the end of the string, use the following string slicing notation. ... In the following example, we will take a string in mystring, and get the substring that starts at the index 6, and ends at the end of the given string. mystring ...
🌐
Note.nkmk.me
note.nkmk.me › home › python
Extract a Substring from a String in Python (Position, Regex) | note.nkmk.me
April 29, 2025 - You can extract a substring within the range start <= x < stop using the syntax [start:stop]. If start is omitted, slicing begins from the start of the string. If stop is omitted, it continues to the end of the string.
Find elsewhere
🌐
Tutorial Gateway
tutorialgateway.org › python-substring
Python substring
March 26, 2025 - Before the Space = Coding After the Space = Examples · Here, we use the second argument (starting point) to find the sentence. Next, we are returning the substring before that and after that character. i = 'We are abc group working as a abc Developer in abc company'; end1 = i.find('group') print('Before: ', i[: end1]) print('After: ', i[end1 : ]) end2 = i.find('abc') print('\nBefore: ', i[: end2]) print('After: ', i[end2 : ]) end3 = i.find('abc', 11) print('\nBefore: ', i[: end3]) print('After: ', i[end3 : ]) end4 = i.find('abc', 35) print('\nBefore: ', i[: end4]) print('After: ', i[end4 : ])
🌐
PhoenixNAP
phoenixnap.com › home › kb › devops and development › how to substring a string in python
How to Substring a String in Python | phoenixNAP KB
November 27, 2025 - To create a substring from a specific index to the end, see the following example: quote = "Toto, I have a feeling we're not in Kansas anymore." print(quote[43:]) print(quote[-8:]) In both cases, the code prints the end of a string after a specific ...
🌐
Dot Net Perls
dotnetperls.com › substring-python
Python - Substring Examples - Dot Net Perls
September 20, 2024 - Tip The length of the slice is the difference between the two arguments (subtract the first from the second). string = "ABCDEFGH" # Two-character substring. two = string[ ... This example shows the relation between the start and the end index.
🌐
Codecademy
codecademy.com › docs › python › substrings
Python | Substrings | Codecademy
June 18, 2025 - To specify only an end index, use [:n], where n is the ending position. This will return the first n characters. ... The string method .find() can also be used to find a subset. It returns the index of the first occurrence of the substring.
🌐
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
🌐
Net Informations
net-informations.com › python › basics › substring.htm
Python Substring examples
When using negative numbers for start and stop, counting begins from the last character, and for the optional step argument, the index is decremented accordingly. This flexibility in indexing provides convenient ways to navigate and extract substrings from both ends of the string. ... Python allows obtaining substrings from the right side of the string using negative indexing.
🌐
Vultr
docs.vultr.com › python › examples › get-a-substring-of-a-string
Python Program to Get a Substring of a String | Vultr Docs
December 25, 2024 - -6 starts the slice six characters from the end, up to, but not including, the last character. Note: Python does not have a built-in substring() method similar to other programming languages.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-substring-a-string-in-python
How to Substring a String in Python - GeeksforGeeks
July 23, 2025 - In this example, we are trying ... "Geeks" from the beginning of the string. The slice notation str[:5] starts at index 0 (the first character) and goes up to, but does not include, index 5, resulting in "Geeks". ... In this example we are trying to extract the last portion of the string.we used string slicing to extract the substring "best!". By omitting the end index, the ...
🌐
Career Karma
careerkarma.com › blog › python › python substring: a step-by-step guide
Python Substring: A Step-By-Step Guide | Career Karma
December 1, 2023 - N is equal to the step you specify. step is equal to 1 by default. We’re going to retrieve the first three characters from a string. To do so, we’ll use slicing syntax: ourString = "Python Substring!" print(ourString[0:3])
🌐
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 - Well, what we did there was tell Python to get the start element all the way to the end element. It's completely valid. It also creates a new copy of the string or array. You could use this if you need a copy of the original to modify! Another example, using extended slicing, can get the sub-string in reverse order.