>>> 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
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-substring-a-string-in-python
How to Substring a String in Python - GeeksforGeeks
July 23, 2025 - This article will discuss how to substring a string in Python. ... 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.
Discussions

How do I remove a substring from a string by indicating what I am wanting to remove with a string variable?
replace is a method of str so by calling it by itself it's treating the first argument as the str instance. It's equivalent to substring.replace(''), which, as you can see from the error message you're getting, is missing an argument. You need: your_string.replace(substring, '') More on reddit.com
🌐 r/learnpython
10
30
July 29, 2022
What is the most efficient way to find substrings in strings?
Easiest solution is to just use the 'in' operator like this: if "Leonardo DiCaprio" in article_title: return True And according to the top answer here it's also the fastest: whats-a-faster-operation-re-match-search-or-str-find Edit:cant get that return statement to indent for the life of me, you get the idea More on reddit.com
🌐 r/learnpython
22
4
January 11, 2022
Finding ordered substring in string using a function
If I understand you correctly, you want something like this? def ordered_sub_found(string: str, sub: str) -> bool: for char in sub: position = string.find(char) if position >= 0: string = string[position:] else: # find returns -1 when it does not find the character # so the next character was not found in the remaining # part of the string return False return True tests = ( ('this should pass', 'hiodps', True), ('this should fail', 'hiholfia', False), ) for string, sub, expected in tests: assert ordered_sub_found(string, sub) is expected More on reddit.com
🌐 r/learnpython
6
5
January 12, 2022
The simplest way to extract a substring from a string
I personally would either use string slicing or a regex. For example, the slicing version would probably look something like substring = string[string.find("(") + 1:string.find(")")] and the regex you would use would probably look something like this: re.search(r'\((.*?)\)', string).group(1) More on reddit.com
🌐 r/learnpython
5
1
May 27, 2021
🌐
Codecademy
codecademy.com › docs › python › substrings
Python | Substrings | Codecademy
June 18, 2025 - The string method .find() can also be used to find a subset. It returns the index of the first occurrence of the substring. If the substring is not found, it returns -1. ... Use slicing for efficiency: String slicing is optimized in Python and should be preferred over loops for extracting substrings.
🌐
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 - Note: Learn how to perform string repetition in Python using several different methods. The following code shows how to make a substring from the beginning up to an index using slicing:
🌐
IONOS
ionos.com › digital guide › websites › web development › python substring
How to create and edit Python substrings - IONOS
July 19, 2023 - 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 char­ac­ters from the 0th index up to and including the 5th index. In this example, the “result” variable contains the word “Python”.
🌐
Server Academy
serveracademy.com › blog › python-substring-tutorial
Python Substring Tutorial Blog | Server Academy
July 10, 2024 - Manipulating Python strings is a common task you'll need to do regularly. One key skill is working with substrings, which are smaller parts of a larger string…
🌐
Dot Net Perls
dotnetperls.com › substring-python
Python - Substring Examples - Dot Net Perls
September 20, 2024 - string = "ABCDEFGH" # Two-character substring. two = string[ ... This example shows the relation between the start and the end index.
Find elsewhere
🌐
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 also use regular expressions to extract a substring using the re module in Python. You can use the re.search() function to search the string for a specific pattern. The method returns a match object if a match is found. You can then use the .group() method on the match object to extract the matching substring. For example, to extract the price from a string:
🌐
Net Informations
net-informations.com › python › basics › substring.htm
Python Substring examples
In Python, strings are sequences of bytes that represent Unicode characters. Accessing individual characters within a string can be achieved using array-like indexing. Similar to array data types, where items are associated with an index number, each character in a string is also associated with an index number, starting from 0. Substring-specific methods like substring() or substr() are not available.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Extract a Substring from a String in Python (Position, Regex) | note.nkmk.me
April 29, 2025 - The following example uses integer division //, which truncates the decimal part. s = 'abcdefghi' print(len(s)) # 9 # print(s[len(s) / 2]) # TypeError: string indices must be integers print(s[len(s) // 2]) # e print(s[:len(s) // 2]) # abcd ...
🌐
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.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-string-substring
Python String Substring | DigitalOcean
August 4, 2022 - def find_all_indexes(input_str, substring): l2 = [] length = len(input_str) index = 0 while index < length: i = input_str.find(substring, index) if i == -1: return l2 l2.append(i) index = i + 1 return l2 s = 'This Is The Best Theorem' print(find_all_indexes(s, 'Th')) ... You can checkout complete ...
🌐
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 - Understand that negative indices can be used to slice strings from the end. Use a negative start index to get a substring starting from the end. ... Here, the substring world is extracted using negative indices. -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.
🌐
Dive into Python
diveintopython.org › home › learn python programming › variables in python › text and string variables in python › substring operations in python
Substring Operations in Python: Slicing, Subset, Reverse, Split, Replace
July 13, 2025 - Here is an example: my_string = "Hello World!" new_string = my_string[:-1] print(new_string) # Output: "Hello World" To check if a Python string contains a specific substring, you can use the in keyword or the find() method.
🌐
LearnPython.com
learnpython.com › blog › substring-of-string-python
How to Get a Substring of a String in Python | LearnPython.com
April 26, 2022 - Since the input to the join() method takes a list, you can do a list comprehension to create a substring from all words with a length equal to 4, for example. For those of you looking for a more challenging exercise, try this for yourself. We’ll also show you a different method to do this later in the article. If you want to know how to write strings to a file in Python, check out this article.
🌐
FROMDEV
fromdev.com › home › 2025 › april
How to Get a Substring of a String in Python: Slicing and Methods - FROMDEV
April 10, 2025 - A substring is any portion of a larger string. For example, in the string "PythonProgramming", "Python" and "Program" are substrings.
🌐
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 - You can extract a substring in Python using slicing, with the format: YourString[StartingIndex:StoppingIndex:CharactersToSkip]. Often, programmers have data that they want to split up into different parts.
🌐
AskPython
askpython.com › home › python string substring
Python String Substring - AskPython
August 6, 2022 - str = 'Engineering Discipline' sub1 = str[:3] sub2 = str[6:] print ("Resultant substring from the start: ", sub1) print ("Resultant substring from the end: ", sub2) ... from itertools import combinations str1 = "Safa" print("The original string ...
🌐
Simplilearn
simplilearn.com › home › resources › software development › what is substring in python and how to create a substring
What is Substring in Python and How to Create a Substring
May 5, 2025 - Part of a string is known as a substring. Learn different methods of creating substring in python with syntax and examples in this tutorial. Start now!
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
iO Flood
ioflood.com › blog › python-substring-quick-reference
Python Substring Quick Reference
January 31, 2024 - Let’s explore a few more examples of substring extraction in different scenarios. string = "Hello, Python!" substring = string[:5] print(substring) # Outputs: Hello