>>> 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.
Discussions

Python: best way to find and extract substring from a string?
Your post is kind of messed up because you didn't format your example properly as a code block. But based on the way your Markdown formatting came out, I'm guessing that the three strings you're looking for are on lines by themselves that start with a # symbol. In that case you could just do something along the lines of: lines = string.split('\n') heading_lines = [l for l in lines if l.startswith('#')] title, description, results = [l.strip('#').strip() for l in heading_lines[:3]] More on reddit.com
๐ŸŒ r/learnprogramming
10
6
October 9, 2024
How to find all occurrences of a substring in a string while ignore some characters in Python?
You could use re for this. ex import re long_string = 'this is a t`es"t. Does the test work?' small_string = "test" chars_to_ignore = ['"', '`'] print(re.findall(f"[{''.join(chars_to_ignore)}]*".join(small_string), long_string)) More on reddit.com
๐ŸŒ r/learnpython
6
1
July 25, 2024
Medium Leetcode question - Longest Substring Without Repeating Characters
I'm a bit surprised that this does not exceed the time limit. You're restarting the loop once for each possible starting position, right? That makes the runtime quadratic. And yes, there is a better way. The problem with your original code was that it assumed that, if we find a duplicate, the next duplicate-free substring to consider must start here. But as you found out, that's not true: If we take "abacdef", the longest duplicate-free substring is "bacdef", not "acdef". So how do we find this without trying every starting position? By taking into account the last occurrence of the duplicated character. That is, after finding the duplicate 'a', we don't continue with an empty substring, we continue with a substring that starts after the previous 'a'. More on reddit.com
๐ŸŒ r/learnpython
2
3
June 22, 2024
count the occurrence of a certain substring in multiple strings(from inputs)

you may find the count method handy.

string.count(substring)

More on reddit.com
๐ŸŒ r/learnpython
6
2
April 21, 2021
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-substring-how-to-slice-a-string
Python Substring โ€“ How to Slice a String
July 27, 2021 - We can define a substring as a sequence of characters within a string. From the previous example, Python substrings can be "Friday", "at", and "meet", for example.
๐ŸŒ
Csuk
coder.csuk.io โ€บ coder_course_page โ€บ string-methods-in-python-find-replace-split-strip-left-mid-right
String Methods in Python (find, replace, split, strip, left, mid, right) โ€“ CSUK:Coder
Understanding how to find, replace, split, and slice strings will give you the ability to handle almost any text-related problem in Python. Hereโ€™s a breakdown of the string methods we'll cover: .find() โ€“ This method is used to locate a substring within a string.
๐ŸŒ
Server Academy
serveracademy.com โ€บ blog โ€บ python-substring-tutorial
Python Substring Tutorial - Server Academy
May 28, 2024 - For your convenience we have embedded a Python 3 fiddler here on this post so you can practice what you learn in this blog post. A substring is simply a smaller string that exists within a larger string. Think of it like a piece of a bigger puzzle.
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ re.html
re โ€” Regular expression operations
2 days ago - In string-type repl arguments, in addition to the character escapes and backreferences described above, \g<name> will use the substring matched by the group named name, as defined by the (?P<name>...) syntax. \g<number> uses the corresponding group number; \g<2> is therefore equivalent to \2, but isnโ€™t ambiguous in a replacement such as \g<2>0.
Find elsewhere
๐ŸŒ
IONOS
ionos.com โ€บ digital guide โ€บ websites โ€บ web development โ€บ python substring
How to create and edit Python substrings
July 19, 2023 - The syntax of slicing in Python looks like this: ... First, we created a string called โ€œsโ€. In the second line of the code example, you can see how slicing works. After the name of the string, there are square brackets. Inside the brackets, youโ€™ll find the start index 0 followed by a colon and then the end index 6. The โ€œresultโ€ variable then stores a substring of โ€œsโ€, which consists of the characters from the 0th index up to and including the 5th index.
๐ŸŒ
LearnDataSci
learndatasci.com โ€บ solutions โ€บ python-substring
Quickly Substring a String in Python โ€“ LearnDataSci
Using this approach will return a substring with everything that comes before the end index. See below for an example of this using example_string (note that this produces the same result as example_string[0:7] in the intro): example_string = 'example_string' example_string[:7] ... Notice that the second 'e' is at index 6, so using an end point of 7, Python returns a substring containing everything that comes before.
๐ŸŒ
Agnostic Development
agnosticdev.com โ€บ content โ€บ how-find-substring-string-python
How to Find a Substring in a String with Python | Agnostic Development
November 23, 2018 - The code samples in this tutorial will cover Python 2.7.13 and Python 3.6.1 and will be tested on Ubuntu (16.04) Linux and macOS Sierra 10.12.4. I have not tested the code in a Window's environment, but if you are using a Linux subsystem on Windows with Python available, the results should be fairly similar, but cannot be guaranteed. ... The most straightforward and widely available example of how to determine if a substring is available in a parent string is by performing a boolean check to determine if a parent string is containing a substring value.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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 to extract the starting word from the string.we used string slicing to extract the substring "Geeks" from the beginning of the string.
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ python-substring-quick-reference
Python Substring Quick Reference
January 31, 2024 - Remember, Python indexing starts at 0, so index 2 is the third character and slicing is exclusive of the end index. ... A substring is a portion of a string. It can be as short as a single character or as long as the entire string itself.
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ introduction.html
3. An Informal Introduction to Python โ€” Python 3.14.3 documentation
In addition to indexing, slicing is also supported. While indexing is used to obtain individual characters, slicing allows you to obtain a substring:
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-string-substring
Python String Substring | DigitalOcean
August 4, 2022 - A substring is the part of a string. Python string provides various methods to create a substring, check if it contains a substring, index of substring etc. โ€ฆ
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ stdtypes.html
Built-in Types โ€” Python 3.14.3 documentation
6 days ago - 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:
๐ŸŒ
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.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ examples โ€บ substring-of-string
Python Program to Get a Substring of a String
# prints "love" print(my_string[2:6]) # prints "love python." print(my_string[2:]) # prints "I love python" print(my_string[:-1]) ... String slicing works similar to list slicing. The working of above code can be understood in the following points. [2:6] You need to specify the starting index and the ending index of the substring.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ string.html
string โ€” Common string operations
Template strings provide simpler string substitutions as described in PEP 292. A primary use case for template strings is for internationalization (i18n) since in that context, the simpler syntax and functionality makes it easier to translate than other built-in string formatting facilities in Python.
๐ŸŒ
Squash
squash.io โ€บ how-to-get-substring-in-python-python-substring-tutorial
How To Get Substrings In Python: Python Substring Tutorial
October 7, 2023 - Getting substrings is a common task in various scenarios, such as data manipulation, text processing, and parsing. To get a substring in Python, you can make use of string slicing. Python provides a simple and intuitive way to extract a part ...