>>> 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 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.
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.
Videos
07:40
string substring (Python) - YouTube
05:12
Python Program #59 - Get a Substring of a String in Python - YouTube
17:38
Identifying a Substring Within a Python String - YouTube
How to Print All Possible Substrings of a String in Python ...
06:33
18. Find a Substring in a String: Hackerrank | Python Solution ...
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
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.
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.
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".
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.