>>> foo = "file.txt" >>> foo[:] 'file.txt' >>> foo[1:] 'ile.txt' >>> foo[-4] '.' >>> foo[-4:] '.txt' Answer from 1544756405 on reddit.com
🌐
GeeksforGeeks
geeksforgeeks.org › python › string-slicing-in-python
String Slicing in Python - GeeksforGeeks
Negative indexing makes it easy to get items without needing to know the exact length of the string. ... s[-4:] slices the string starting from the 4th character from the end ('m') to the end of the string.
Published   July 12, 2025
🌐
W3Schools
w3schools.com › python › python_strings_slicing.asp
Python - Slicing Strings
You can return a range 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. Get the characters from position 2 to position ...
Discussions

python - Slice to the end of a string without using len() - Stack Overflow
With string indices, is there a way to slice to end of string without using len()? Negative indices start from the end, but [-1] omits the final character. word = "Help" word[1:-1] # But I More on stackoverflow.com
🌐 stackoverflow.com
how to slice a string from the end
>>> foo = "file.txt" >>> foo[:] 'file.txt' >>> foo[1:] 'ile.txt' >>> foo[-4] '.' >>> foo[-4:] '.txt' More on reddit.com
🌐 r/learnprogramming
28
2
February 19, 2023
String slice explanation, please? I don't understand the output.
It seems that you don't understand the meaning of "step". I will explain: In the third row, you have inserted a step value of 2. This means you will begin at the character 3 and end at character 7, printing every other character (3,5,7) In the fourth row, you will print every third character beginning at 3 (3,6). Keep in mind that your "stop" value is not inclusive, so your string slice will only go up to position 7 in the string More on reddit.com
🌐 r/learnpython
8
3
April 3, 2016
Why slices work the way they are?
It makes sense with a bit of practice. You should think about them differently. It's not a direct access to an index. It's a range. # 0 1 2 3 <--- indices # a b c d # 0 1 2 3 4 <--- slicing # a b c d # so.... elements = ['a', 'b', 'c', 'd'] >>> elements[:3] ['a', 'b', 'c'] More on reddit.com
🌐 r/learnpython
61
136
September 10, 2020
🌐
Reddit
reddit.com › r/learnprogramming › how to slice a string from the end
r/learnprogramming on Reddit: how to slice a string from the end
February 19, 2023 -

hi, i'm playing with string slice and i don't understand some things. i know that the [-1] is the last charachter, [-2] the penultimate etc. so, i have a string like "file.txt" and i want to exctract only the ".txt" part of it. i tried with string[-1:-4] but i get an empty string; then i tried with string[:len(string)-5:-1] but i get "txt.". i don't know what i'm doing wrong, can someone explain it to me? thank you

Problem with String Slicing in Python Aug 27, 2022
r/learnprogramming
3y ago
How to split a string based on this pattern? Feb 28, 2023
r/learnpython
3y ago
Can someone explain string splicing in detail? Aug 13, 2021
r/learnpython
4y ago
A Comprehensive Guide to Slicing in Python Feb 1, 2022
r/Python
4y ago
How does strip() method work for Python Strings? Dec 28, 2018
r/learnpython
7y ago
More results at reddit.com
🌐
Learn By Example
learnbyexample.org › python-string-slicing
Python String Slicing - Learn By Example
April 20, 2020 - # Slice first three characters from the string S = 'ABCDEFGHI' print(S[:3]) # ABC · Whereas, omitting the stop index extends the slice to the end of the string.
🌐
LearnPython.com
learnpython.com › blog › string-slicing-in-python
String Slicing in Python: A Complete Guide | LearnPython.com
April 15, 2024 - Python provides more flexible ways to slice a string. By default, string slice includes all the characters between the given starting and ending point. We can change this behavior using the step size parameter.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-slice-string
Python slice string | DigitalOcean
August 3, 2022 - Python string supports slicing to create substring. Note that Python string is immutable, slicing creates a new substring from the source string and original string remains unchanged. ... The slicing starts with the start_pos index (included) and ends at end_pos index (excluded).
Find elsewhere
🌐
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 - Slicing: How to extract a substring (a “slice”) from a larger string using the [start:end:step] syntax. We saw how start is inclusive, end is exclusive, and step determines the increment. Key Differences: The distinction between indexing (which returns a single character and raises an ...
🌐
W3Schools
w3schools.com › python › gloss_python_string_slice.asp
Python Slice Strings
You can return a range 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. Get the characters from position 2 to position 5 (not included):
🌐
Python Central
pythoncentral.io › cutting-and-slicing-strings-in-python
Cutting and slicing strings in Python - Python Central
September 6, 2023 - The first index, if omitted, defaults to 0, so that your chunk starts from the beginning of the original string; the second defaults to the highest position in the string, so that your chunk ends at the end of the original string. Omitting both indices isn't likely to be of much practical use; as you might guess, it simply returns the whole of the original string. [python] >>> s[4:] 'Quijote' # Returns from pos 4 to the end of the string >>> s[:4] 'Don ' # Returns from the beginning to pos 3 >>> s[:] 'Don Quijote' [/python]
🌐
Real Python
realpython.com › lessons › string-slicing
String Slicing (Video) – Real Python
When doing string slicing, you can also specify a stride in your string slice. 05:46 It’s adding an additional colon (:) and a third index designates what the stride will be. It’s also called a step. So in this case, if our slice was from the beginning, 0, to the end of our string here, 7, and then followed a step of 2, it would start with the very first item, which is 0, and then skip—taking two steps—to grab index 2, and then skip ahead two, and grab index 4, and then go to the end here, index 6.
Published   October 1, 2019
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python string slice with examples
Python String Slice with Examples - Spark By {Examples}
May 21, 2024 - You can also extract a sub-string (also called a “slice”) from a python string using the slice notation with syntax [start:stop:step]. Here, the start index is the starting position of the slice, and the stop index is the ending position ...
🌐
AskPython
askpython.com › home › how to slice strings in python?
How to Slice Strings in Python? - AskPython
August 6, 2022 - We can easily slice a given string by mentioning the starting and ending indices for the desired sub-string we are looking for. Look at the example given below, it explains string slicing using starting and ending indices for both usual and ...
🌐
Upskillcampus
upskillcampus.com › home › slicing strings in python: a step-by-step tutorial
Slicing Strings in Python: A Step-by-Step Tutorial - Latest Insights & Guides | Career Upskilling Blogs
January 2, 2025 - Let’s show how string slicing works and how you use it effectively. ... Start: The index where the slice begins (inclusive). End: The index where the slice ends (exclusive). Moreover, the character at the end index is not included. ... This returns "World" because it starts at index 7 and ends just before index 12 (excluding index 12). ... Omitting the Start: If you don’t specify a start index, Python assumes it starts from the beginning.
🌐
InterServer
interserver.net › home › python › how to slice strings in python: step-by-step tutorial
How to Slice Strings in Python: Step-by-Step Tutorial - Interserver Tips
April 23, 2026 - This will give “arad”, which starts at index 1 and ends at index 4. Slicing like this helps you pick exactly the characters you need. The step parameter lets you skip characters in a string. It is written as the third value in the slice: [start:stop:step]. For example, if text = "PythonProgramming", you can get every second character using:
🌐
PhoenixNAP
phoenixnap.com › home › kb › devops and development › string slicing in python
String Slicing in Python | phoenixNAP KB
October 24, 2023 - Provide all three values like in the following example: quote = "There's no place like home." print(quote[3:-1:2]) The slicing starts the substring at the fourth character and goes to the end, printing every other character.
🌐
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 - To create a substring using the Python slice function, first create a slice object using the slice constructor. Pass the start index, end index, and stride as arguments to the constructor.
🌐
freeCodeCamp
freecodecamp.org › news › python-substring-how-to-slice-a-string
Python Substring – How to Slice a String
July 27, 2021 - In this example, you will slice the last 4 characters from the string. Here you use the negative index to start slicing from the end of the string.
🌐
Mimo
mimo.org › tutorials › python › how-to-slice-a-string-in-python
How to Slice a String in Python
Learn how to slice a string in Python with s[start:end] and s[start:end:step] so you can extract prefixes, suffixes, ranges, or patterns without loops.
🌐
DEV Community
dev.to › balapriya › string-slicing-in-python-explained-5edg
String Slicing in Python, Explained - DEV Community
September 14, 2022 - If step = -1 you get a slice starting from the end of the string, and including every character. This can be super handy when you'd like to reverse a string, like this: ... To sum up, <str>[start:stop:step] is the syntax to obtain string slices ...