DigitalOcean
digitalocean.com › community › tutorials › how-to-index-and-slice-strings-in-python-3
How To Index and Slice Strings in Python | DigitalOcean
September 29, 2025 - Key Differences: The distinction between indexing (which returns a single character and raises an IndexError if out of bounds) and slicing (which returns a substring and gracefully handles out-of-bounds requests by returning an empty string). String Reversal: The efficient and Pythonic way ...
Today I leaned that in python, slicing a substring with an index that is out of range does not result in an error.
Yeah, the reasoning makes more sense with lists. See this Stack Overflow post: https://stackoverflow.com/a/9490148/4637427 More on reddit.com
Algorithm complexity with strings and slices
The python page on time-complexity shows that slicing lists has a time-complexity of O(k), where "k" is the length of the slice. That's for lists, not strings, but the complexity can't be O(1) for strings since the slicing must handle more characters as the size is increased. At a guess, the complexity of slicing strings would also be O(k). We can write a little bit of code to test that guess:
import time
StartSize = 2097152
size = StartSize
for _ in range(10):
# create string of size "size"
s = '*' * size
# now time reverse slice
start = time.time()
r = s[::-1]
delta = time.time() - start
print(f'Size {size:9d}, time={delta:.3f}')
# double size of the string
size *= 2This uses a simple method of timing. Other tools exist, but this is simple. When run I get:
$ python3 test.py Size 2097152, time=0.006 Size 4194304, time=0.013 Size 8388608, time=0.024 Size 16777216, time=0.050 Size 33554432, time=0.098 Size 67108864, time=0.190 Size 134217728, time=0.401 Size 268435456, time=0.808 Size 536870912, time=1.610 Size 1073741824, time=3.192
which shows the time doubles when doubling the size of the string for each reverse slice. So O(n) (k == n for whole-string slicing).
Edit: spelling.
More on reddit.com[Beginner] Question about string slicing and indexing
Why does [::1] reverse a string in Python?
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge. If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options: Limiting your involvement with Reddit, or Temporarily refraining from using Reddit Cancelling your subscription of Reddit Premium as a way to voice your protest. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
Videos
09:16
Python for Beginners: Part 4 (String Indexing and Slicing) - YouTube
03:24
String Indexing (Video) – Real Python
07:28
String indexing in Python is easy ✂️ - YouTube
07:23
Python for Beginners — Part 25: String Indexing - YouTube
08:24
Python String Indexing Explained | Accessing Characters with Indexes ...
09:44
How to Index and Slice Strings in Python - YouTube
W3Schools
w3schools.com › python › python_strings_slicing.asp
Python - Slicing Strings
Remove List Duplicates Reverse ... of characters by using the slice syntax. Specify the start index and the end index, separated by a colon, to return a part of the string....
UC Berkeley Statistics
stat.berkeley.edu › ~spector › extension › python › notes › node19.html
Indexing and Slicing
>>> what = 'This parrot is dead' ... piece of a string (known as a slice), use a subscript consisting of the starting position followed by a colon (:, finally followed by one more than the ending position of the slice you want to extract....
Pluralsight
pluralsight.com › labs › aws › indexing-and-slicing-python-strings
Indexing and Slicing Python Strings
Accessing characters, whether they are single or multiple, is an essential skill for working with strings in any programming language. In Python, we do this by indexing and slicing the string. In this hands-on lab, we'll go through writing some code to demonstrate that we understand how to ...
Exam-Labs
exam-labs.com › home
Mastering String Indexing and Slicing Techniques in Python - Exam-Labs
December 10, 2025 - Python’s slicing syntax accommodates the omission of any or all indices, defaulting intelligently to facilitate common use cases. Omitting both start and stop, as in string[:], produces a shallow copy of the entire string. Leaving the start blank causes slicing to begin at the start of the string, while an absent stop index extends the slice to the string’s end.
Hostman
hostman.com › tutorials › slicing and indexing strings in python
Slicing and Indexing Strings in Python
December 10, 2024 - Indexing allows you to access individual characters in a string. Each character in a Python string is assigned an index starting from 0 for the first character and incrementing by 1 for each subsequent character.
Practicise
practicise.com › home › lessons › string indexing and slicing
String Indexing and Slicing - Python
November 12, 2024 - text = "Python" # Extracting characters from index 1 to index 4 (end is exclusive) print(text[1:4]) # "yth" (characters at index 1, 2, and 3) # Extracting from the start to index 3 (end is exclusive) print(text[:3]) # "Pyt" # Extracting from index 2 to the end of the string print(text[2:]) # "thon" # Extracting the whole string print(text[:]) # "Python" ... text = "Python" # Extracting the last character (negative index -1) print(text[-1]) # "n" # Slicing the last 3 characters (index -3 to -1) print(text[-3:]) # "hon" # Extracting the substring "yth" from the middle using negative indices print(text[-5:-2]) # "yth" You can also specify a step value in slicing, which will determine how many steps the slice should skip between indices.
Manifoldapp
cuny.manifoldapp.org › read › how-to-code-in-python-3 › section › 93337ae8-329e-493b-b15a-7fe26f12f037
How To Index and Slice Strings | How To Code in Python 3 | Manifold @CUNY
We can do so by creating a slice, which is a sequence of characters within an original string. With slices, we can call multiple character values by creating a range of index numbers separated by a colon [x:y]: ... When constructing a slice, as in [6:11], the first index number is where the ...
Hostman
hostman.com › tutorials › indexing and slicing strings in python 3
String Indexing and Slicing in Python 3: The Complete Guide
December 10, 2024 - In this example, 1 is the start index, and 6 is the end index. The slice includes characters from index 1 to 5 (the end index is excluded). If you want to extract a substring starting from index 0, you can omit the start index: print('Characters from index 0 to 6: ', string[:6]) ... Similarly, you can omit the end index if you want a slice that goes to the end of the string. Python also allows the use of negative indices when slicing.
CloudSigma
blog.cloudsigma.com › home › customers › python 3 tutorial: indexing and slicing strings
Python 3 Tutorial: Indexing and Slicing Strings • CloudSigma
June 28, 2022 - In this cloud tutorial, we will explore how you can access strings through indexing and slicing them through their sequences on Python 3. Introduction Python is one of the most popular programming languages across the world. It uses a data type that comprises sequences made up of individual char
Tutorialspoint
tutorialspoint.com › python › python_slicing_strings.htm
Python Slicing Strings
Python defines ":" as string slicing operator. It returns a substring from the original string. Its general usage is as follows − ... The ":" operator needs two integer operands (both of which may be omitted, as we shall see in subsequent ...
Medium
medium.com › @sanapjatin_47006 › understanding-string-indexing-and-slicing-in-python-803b8ac33063
Understanding String Indexing and Slicing in Python | by Sanapjatin | Medium
October 30, 2025 - You can reverse a string using a negative step ([::-1]), skip characters, or even combine slices to form new words. ... Mastering string indexing and slicing is not just about extracting characters — it’s about manipulating data efficiently. These concepts are widely used in text analysis, data cleaning, and building algorithms that deal with textual input. Whether you are trimming user input, parsing information, or reversing a sentence, indexing and slicing provide the foundation for effective string handling in Python.
Codefinity
codefinity.com › courses › v2 › 04fb3c65-4043-4ee3-a995-cba5bdd807af › b427d60d-9690-43e9-8110-556cc8891a2c › 7b6b9d37-e031-4e8d-ac83-ec2f3b893ebb
Learn Indexing and Slicing | Strings
Also, strings are immutable, so you can read s[i] but not assign to it. 12 s = "python" s[0] = 'P' # TypeError: 'str' object does not support item assignment ... A slice uses start:stop:step and returns a new string.