>>> 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 OverflowIONOS
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 substrings 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, ...
Videos
03:57
How To Find The Last Occurrence Of A Substring In Python - YouTube
06:47
🐍 Python Tutorial #26: String Slicing - YouTube
00:37
How To Check If A String Ends With A Substring In Python - YouTube
01:16
PYTHON : How do I remove a substring from the end of a string? ...
05:12
Python Program #59 - Get a Substring of a String in Python - YouTube
17:38
Identifying a Substring Within a Python String - YouTube
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.
Top answer 1 of 16
3835
>>> 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.
2 of 16
508
Just for completeness as nobody else has mentioned it. The third parameter to an array slice is a step. So reversing a string is as simple as:
some_string[::-1]
Or selecting alternate characters would be:
"H-e-l-l-o- -W-o-r-l-d"[::2] # outputs "Hello World"
The ability to step forwards and backwards through the string maintains consistency with being able to array slice from the start or end.
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 ...
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 : ])
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.
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.
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 ...
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.