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

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
🌐 r/learnpython
4
8
March 28, 2024
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 *= 2

This 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
🌐 r/learnpython
4
3
December 1, 2019
[Beginner] Question about string slicing and indexing
🌐 r/learnpython
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
🌐 r/learnprogramming
15
12
September 21, 2023
🌐
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....
🌐
Linode
linode.com › docs › guides › how-to-slice-and-index-strings-in-python
How to Slice and Index Strings in Python | Linode Docs
January 28, 2022 - String slicing is a powerful operation for creating a substring from the original string. In addition to extracting a string consisting of consecutive characters, a string slice can select every nth letter or even work in reverse.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-index-and-slice-strings-in-python
How to Index and Slice Strings in Python? - GeeksforGeeks
July 15, 2025 - String Indexing allows us to access individual characters in a string. Python treats strings like lists where each character is assigned an index, starting from 0. We can access characters in a String in Two ways : ... In this type of Indexing ...
🌐
freeCodeCamp
freecodecamp.org › news › slicing-and-indexing-in-python
Slicing and Indexing in Python – Explained with Examples
December 11, 2025 - They help you access specific elements in a sequence, such as a list, tuple or string. By using these techniques, you can extract substrings from strings, filter lists, and extract columns ...
🌐
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.
Find elsewhere
🌐
DEV Community
dev.to › jobreadyprogrammer › strings-in-python-indexing-and-slicing-for-beginners-2387
Strings in Python: Indexing and Slicing for Beginners - DEV Community
December 20, 2024 - If you only want to slice from a given index to the end while skipping characters, simply omit the end value. You can reverse a string by using slicing with a step of -1. sentence = "abcdefg" # Reverse the string print(sentence[::-1]) # Output: ...
🌐
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.
Price   $
Address   1999 Harrison St 1800 9079, 94612, Oakland
🌐
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 ...
🌐
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.
🌐
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
🌐
Medium
medium.com › @tirulesnar › python-string-indexing-and-slicing-d69ba972bd91
🐍 Python String Indexing and Slicing | by Tirupathi Rao Guggilapu | Medium
September 18, 2025 - If you try to access an index that doesn’t exist, Python throws an IndexError. ... Slicing allows you to extract parts of a string using indexes. It’s like “cutting” a substring out of a bigger string.
🌐
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.
Price   $
Address   1999 Harrison St 1800 9079, 94612, Oakland
🌐
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 ...
🌐
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.
🌐
OpenStax
openstax.org › books › introduction-python-programming › pages › 8-2-string-slicing
8.2 String slicing - Introduction to Python Programming | OpenStax
March 13, 2024 - String slicing is used when a programmer must get access to a sequence of characters. Here, a string slicing operator can be used. When [a:b] is used with the name of a string variable, a sequence of characters starting from index a (inclusive) ...
🌐
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.
🌐
DEV Community
dev.to › abys_learning_2024 › python-indexing-and-slicing-2moh
Python - Indexing and Slicing - DEV Community
July 24, 2024 - This way, we can create smaller portions of the cake (or string) exactly how we like them! In Python, slicing a string lets us grab specific parts of it by specifying where to start and where to end within the string.