>>> 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
Since strings are arrays, we can loop through the characters in a string, with a for loop. ... Learn more about For Loops in our Python For Loops chapter.
Videos
17:38
Identifying a Substring Within a Python String - YouTube
05:12
Python Program #59 - Get a Substring of a String in Python - YouTube
02:00
Check if a Python String Contains a Substring (Overview) (Video) ...
03:31
Python Substring - YouTube
07:40
string substring (Python) - YouTube
04:28
Python - String Splicing and Substrings Tutorial with Examples ...
Codecademy
codecademy.com › docs › python › substrings
Python | Substrings | Codecademy
June 18, 2025 - A Python substring is a sequence of characters that are part of an original string. In Python, substrings can be obtained by using the slicing feature on a string variable.
GeeksforGeeks
geeksforgeeks.org › python › how-to-substring-a-string-in-python
How to Substring a String in Python - GeeksforGeeks
July 23, 2025 - 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.
Real Python
realpython.com › python-string-contains-substring
How to Check if a Python String Contains a Substring – Real Python
December 1, 2024 - Note: If you want to check whether the substring is not in the string, then you can use not in: ... Because the substring "secret" is present in raw_file_content, the not in operator returns False. When you use in, the expression returns a Boolean value: ... In this code snippet, you use the membership operator to check whether "secret" is a substring of raw_file_content. If it is, then you’ll print a message to the terminal. Any indented code will only execute if the Python string that you’re checking contains the substring that you provide.
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.
CodeSignal
codesignal.com › learn › courses › practicing-string-operations-and-type-conversions-in-python › lessons › exploring-substring-search-in-python-strings
Exploring Substring Search in Python Strings
We use the built-in zip() function to create pairs of original strings and substrings. We then use the find() method to find the first occurrence of each substring in its related original string. Python string objects have a built-in method called str.find(substring, starting_index=0), which comes in handy here.
Facebook
facebook.com › groups › 1939366696159868 › posts › 5994548140641683
How do I get a substring of a string in Python?
We cannot provide a description for this page right now
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
In the next section, we’ll discover how to use the string .find() method to check for a substring in a string. The .find() method in Python is another built-in method that we can use to locate substrings within a string.
Python.org
discuss.python.org › python help
Use of pipe operator to search for substring in string - Python Help - Discussions on Python.org
June 2, 2022 - current scenario - 1) ('c' or 'a') in 'ab' gives, False as ('c' or 'a') evaluates to 'c' the substring check could be obtained, but need to write it using any builtin any([(i in 'ab') for i in ('c', 'a')]) gives, True in a match case statement, one could check for exact match of a string, x = 'ab' match x: case 'ab' | 'c': print(1) gives 1 but this pipe operator does not work for strings outside of match case statement. expected scenario - pipe operator works for strings ...
Reddit
reddit.com › r › learnprogramming › comments › 1s0nd0 › python_stringsubstring_really_simple_driving_me
[Python] String-substring- really simple, driving me crazy
We cannot provide a description for this page right now
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 - Python allows users to extract substring portions from strings through slicing operations. 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.