Assuming a is a string. The Slice notation in python has the syntax -

list[<start>:<stop>:<step>]

So, when you do a[::-1], it starts from the end towards the first taking each element. So it reverses a. This is applicable for lists/tuples as well.

Example -

>>> a = '1234'
>>> a[::-1]
'4321'

Then you convert it to int and then back to string (Though not sure why you do that) , that just gives you back the string.

Answer from Anand S Kumar on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ what does this mean: s[::-1] == s ?
r/learnpython on Reddit: What does this mean: s[::-1] == s ?
April 5, 2012 -

So, i was just wrinting a small function to see if a string is a palindrome.

I came up with this and was happy:

def is_palindrome(to_test):
    if len(to_test)<=1:
        return True
    elif to_test[0] == to_test[-1]:
        return is_palindrome(to_test[1:-1])
    else:
        return False

Until I searched for a smaller and quicker solution and found:

def ispalindrome(s):
    return s[::-1] == s

I do not understand it, can someone explain what this s[::-1] means?

Thank you!

Edit: Spoke to soon, I got it. But I will leave the question here anyway.

๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ what-does-do-in-python
What does [::-1] do in Python?
Remember, negative number for step means "in reverse order". Let us now see examples ? Use the [::-1] to reverse the order of a string in Python ?
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 1838639 โ€บ python-string-question-what-does-1-mean
Python String question: What does [::-1] mean? | Sololearn: Learn to code for FREE!
[2:17:3] means: Start at 2, end ... there's a default. [:] means: The whole thing. [::1] means: Start at the beginning, end when it ends, walk in steps of 1 (which is the default, so you don't even need to write ...
๐ŸŒ
Quora
quora.com โ€บ What-does-1-do-in-Python
What does [::-1] do in Python? - Quora
Answer (1 of 6): It iterates over any iterable thing, backward. That is an example of Slice notation. The full notation is [ start: stop: stride ]. When start is left out it defaults to zero. When stop is left out it defaults to last+1. A negative stride goes from last backwards to start. -2 woul...
๐ŸŒ
Guru99
guru99.com โ€บ home โ€บ python โ€บ [::-1] in python with examples
[::-1] in Python with Examples
July 28, 2025 - Whenever a programmer defines [::-1], it suggests that the program has to traverse from start to end in a given list. You can do indexing in python, which helps to slice and dice an iterable sequence such as a list or string.
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ introduction.html
3. An Informal Introduction to Python โ€” Python 3.14.2 documentation
>>> a, b = 0, 1 >>> while a < 1000: ... print(a, end=',') ... a, b = b, a+b ... 0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987, ... Since ** has higher precedence than -, -3**2 will be interpreted as -(3**2) and thus result in -9. To avoid this and get 9, you can use (-3)**2. ... Unlike other languages, special characters such as \n have the same meaning with both single ('...') and double ("...") quotes.
Top answer
1 of 2
203

Assuming a is a string. The Slice notation in python has the syntax -

list[<start>:<stop>:<step>]

So, when you do a[::-1], it starts from the end towards the first taking each element. So it reverses a. This is applicable for lists/tuples as well.

Example -

>>> a = '1234'
>>> a[::-1]
'4321'

Then you convert it to int and then back to string (Though not sure why you do that) , that just gives you back the string.

2 of 2
56

The notation that is used in

a[::-1]

means that for a given string/list/tuple, you can slice the said object using the format

<object_name>[<start_index>, <stop_index>, <step>]

This means that the object is going to slice every "step" index from the given start index, till the stop index (excluding the stop index) and return it to you.

In case the start index or stop index is missing, it takes up the default value as the start index and stop index of the given string/list/tuple. If the step is left blank, then it takes the default value of 1 i.e it goes through each index.

So,

a = '1234'
print a[::2]

would print

13

Now the indexing here and also the step count, support negative numbers. So, if you give a -1 index, it translates to len(a)-1 index. And if you give -x as the step count, then it would step every x'th value from the start index, till the stop index in the reverse direction. For example

a = '1234'
print a[3:0:-1]

This would return

432

Note, that it doesn't return 4321 because, the stop index is not included.

Now in your case,

str(int(a[::-1]))

would just reverse a given integer, that is stored in a string, and then convert it back to a string

i.e "1234" -> "4321" -> 4321 -> "4321"

If what you are trying to do is just reverse the given string, then simply a[::-1] would work .

Top answer
1 of 2
19

It represents the 'slice' to take from the result. The first element is the starting position, the second is the end (non-inclusive) and the third is the step. An empty value before/after a colon indicates you are either starting from the beginning (s[:3]) or extending to the end (s[3:]). You can include actual numbers here as well, but leaving them out when possible is more idiomatic.

For instance:

In [1]: s = 'abcdefg'

Return the slice of the string that starts at the beginning and stops at index position 2:

In [2]: s[:3]
Out[2]: 'abc'

Return the slice of the string that starts at the third index position and extends to the end:

In [3]: s[3:]
Out[3]: 'defg'

Return the slice of the string that starts at the end and steps backward one element at a time:

In [4]: s[::-1]
Out[4]: 'gfedcba'

Return the slice of the string that contains every other element:

In [5]: s[::2]
Out[5]: 'aceg'

They can all be used in combination with each other as well. Here, we return the slice that returns every other element starting at index position 6 and going to index position 2 (note that s[:2:-2] would be more idiomatic, but I picked a weird number of letters :) ):

In [6]: s[6:2:-2]
Out[6]: 'ge'

The step element determines the elements to return. In your example, the -1 indicates it will step backwards through the item, one element at a time.

2 of 2
2

That's a common idiom that reverses a list.

a = ['a', 'b', 'c', 'd']
b = a[::-1]
print b

['d', 'c', 'b', 'a']

You can read about 'extended slices' here.

Find elsewhere
๐ŸŒ
Google
developers.google.com โ€บ google for education โ€บ python โ€บ python strings
Python Strings | Python Education | Google for Developers
Python does not have a separate character type. Instead an expression like s[8] returns a string-length-1 containing the character. With that string-length-1, the operators ==, <=, ...
๐ŸŒ
CodingBat
codingbat.com โ€บ doc โ€บ python-strings.html
CodingBat Python Strings
Negative Index As an alternative, Python supports using negative numbers to index into a string: -1 means the last char, -2 is the next to last, and so on. In other words -1 is the same as the index len(s)-1, -2 is the same as len(s)-2. The regular index numbers make it convenient to refer ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ what's the difference between [:-1] and [::-1] in python?
r/learnprogramming on Reddit: What's the difference between [:-1] and [::-1] in Python?
February 21, 2022 - And [::-1] js more like I want to move through the list all the elements from the start to finish but I want to move backwards one step at a time ยท ๐Ÿ˜‚๐Ÿ˜‚but donโ€™t quote me Itโ€™s just what I think ... Makes sense, been learning about list iterations since I posted this question and the '::' makes more sense now since it's basically defaulting on those values so it can skip to the step part.
๐ŸŒ
Pierian Training
pieriantraining.com โ€บ home โ€บ what does [:] mean in python? a guide to slicing
What Does [:] Mean in Python? A Guide to Slicing - Pierian Training
June 23, 2023 - This code will return `[3, 4]`. Here, we specified a starting index of -3 (which corresponds to the third last element) and an ending index of -1 (which corresponds to the second last element), resulting in selecting only elements with indices ...
๐ŸŒ
PW Skills
pwskills.com โ€บ blog โ€บ python โ€บ 1 python: what is in 1 in python?
1 Python: What Is In 1 In Python?
November 4, 2025 - ... In Python, -1 is an integer literal representing the negative integer value of one. It is essentially the opposite of 1 and falls within the category of integer data types. Like other integers, -1 is immutable, meaning its value cannot be ...
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-slicing-how-to-slice-an-array
Python Slicing โ€“ How to Slice an Array and What Does [::-1] Mean?
December 8, 2022 - Here, we specify a start of index 2, no end, and a step of -1. Slicing here will start from index 2 which is 3. The negative steps mean the next value in the slice will be at an index smaller than the previous index by 1. This means 2 - 1 which is 1 so the value at this index, which is 2 will be added to the slice. This goes on reversed until it gets to the end of the array which is index 0. The sliced array results in values of 3, 2, and 1. Have you seen this expression anywhere in Python before?
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ what's the meaning and purpose of [-1] in this function?
r/learnpython on Reddit: What's the meaning and purpose of [-1] in this function?
February 29, 2024 -

Basically, the purpose of this code is to break up strings that have camelCases into different strings, and I looked up some code samples online, and I found this solution. The code works, however, I don't quite understand what some of the syntax actually does, even after some research. I made a # comment on each line I didn't understand, explaining my confusion.

I simply just don't want to mindlessly copy code off the internet without understanding what it does!

Also, if possible, are there any lessons online you recommend I goto to learn about this concept to avoid further confusion? Thanks!

def camelCase(str):

words = [[str[0]]]

for c in str[1: ]: #What does str[1: ] do? I tried looking it up but Google isn't helpful

if words[-1][-1].islower() and c.isupper(): #I'm not quite sure what function [-1][-1] plays in this function

words.append(list(c))

else:

words[-1].append(c) #I once again don't understand the function of [-1] in this else statement

return [' '.join(word) for word in words]

Top answer
1 of 4
14
Well, let's break it down! words = [[str[0]]] This is written somewhat strangely, but basically it is setting the words variable to a list containing the first character of the string (note: this code should not use str as a variable name as this is also a type). for c in str[1:]: This for loop is going over the rest of the string character by character. This is called a slice, and essentially it cuts the list into index values. For example, if you had a list [1, 2, 3, 4, 5] and you indexed it with [2:3], the list you'd get back is [3, 4]. This is because you are slicing from index 2 (the third item since indexes start at 0) to index 3 (the 4th item). If you did [2:] instead, your list would be [3, 4, 5], as the empty value means "to the end of the list." If you reversed it with [:2] you'd instead get a list of [1, 2] because you are slicing from 0 (start) to 2. Note that [:2] and [0:2] are functionally the same. In the next section, you have words[-1][-1]. An index of [-1] means "the last element (length - 1). Any time you have a negative index in Python, it means "that many from the length." NOTE: a slice at the end must be blank if you want the last character. If you did [2:] and [2:-1] you'd get different values...in this case [3, 4, 5] and [3, 4] respectively. So what does the words[-1][-1] actually do? Well, remember that words is a list of characters. So you are looking at the last string and last letter of that string, then checking if it is lower case. The if also checks if the next letter is upper case. If it is, we append a new list of characters to the end of the words list. If not, we are instead appending the current character to the current list. This is repeated for the whole string. This list of lists is then converted into a list of individual strings by combining the internal lists into strings instead of a list of characters. You can tell what's going on if you put a print statement before the return. If I had a string, say, testWordOutput, the list before the returning list comprehension looks like this: [['t', 'e', 's', 't'], ['W', 'o', 'r', 'd'], ['O', 'u', 't', 'p', 'u', 't']]. This is actually a somewhat overcomplicated way to do this. You can skip the entire part where you create the internal lists, for example this code: def split_camel_case(original): if not original: # Early return for empty string return [] words = [original[0]] # Start with the first character for char in original[1:]: # If the current character is uppercase and # the previous character is lowercase, # start a new word if char.isupper() and words[-1][-1].islower(): words.append(char) else: # Otherwise, append the current character to the last word words[-1] += char return words This code does the exact same thing but is perhaps a bit more clear in how it works. It still has many of the same elements (the basic concept wasn't wrong) but removes the need for a list comprehension. Hopefully that makes sense, please let me know if you have questions.
2 of 4
11
You should experiment with code in the Python shell, that way you can try these things and see what they do.