>>> 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 ...
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
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
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
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
Videos
13:15
String Slicing | Python Tutorial - YouTube
09:44
How to Index and Slice Strings in Python - YouTube
11:37
Python string slicing ✂️ - YouTube
06:47
🐍 Python Tutorial #26: String Slicing - YouTube
00:59
why python string slices IGNORE end elements - pythontutor.com ...
00:59
it's odd to slice a python string BACKWARDS - pythontutor.com - ...
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
Top answer 1 of 4
4
>>> foo = "file.txt" >>> foo[:] 'file.txt' >>> foo[1:] 'ile.txt' >>> foo[-4] '.' >>> foo[-4:] '.txt'
2 of 4
1
Try "." + (string.split(".")[-1])
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).
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
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:
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.