What does ::-1 mean for python string index? [duplicate] - python

This question already has answers here:
Understanding slicing
(38 answers)
Closed 6 years ago.
I was looking at python code that printed palindromes, and I stumbled upon this line of code:
for i in range(1000, 7, -1):
if (str(i) == str(i)[::-1])
I'm trying to learn Python right now, and I'm just not that familiar with the syntax. Currently, I understand that this line of code checks to see if the first digit of integer i matches its last digit. Does the syntax of this line mean that the index is being incremented in order to check if it's a palindrome? What is the purpose of having two colons?

The colons are separators. Rather than providing a "beginning" and an "end" index, it's telling Python to skip by every -1 objects in the array. It's effectively reversing the array.

Related

printing a str and int in a single statement [duplicate]

This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 2 years ago.
I've begun my first scripting class. I'm am currently stuck in the string formatting section.
The instructions for this problem are, "Write a single statement to print: user_word,user_number. Note that there is no space between the comma and user_number."
They provide part of the code to start,
user_word = str(input())
user_number = int(input())
I've had trouble getting errors combining strings and integers in single statements and I am a bit lost on where to start on this. This is also my first time on stack overflow.
You can do it like this
print(user_word+","+str(user_number))
It should work.
This way, you cast you int to string and then you concatenate.

I don't understand what is within the brackets of some sample code [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 2 years ago.
I have a simple two lines of code but fail to understand what is actually happening.
s = 'We\'re' + 'Here'
print(s[4::2])
The result is just eee. I fail to see how the [4::2] is working to cause the code to print eee.
It is something like [start: end: step], slice addresses can be written as s[start:end:step] and any of start, stop or end can be dropped. So, s[4::2] is starting from 4th and every 2nd element of the sequence(Here you dropped the end part). That's why it is returning eee
s = 'We\'re' + 'Here'
print(s[4::2])

getting error while using count in string "BANANA" to find "ANA" as substring using str.count() inbuild function of python [duplicate]

This question already has answers here:
Python string count not working properly? [duplicate]
(2 answers)
Closed 4 years ago.
when executing the command in python
str="BANANA"
print(str.count("ANA"))
give answer as 1.
but real answer would be 2. how to solve this
It's true that this might be misleading most tutorial sites say that count returns the number of occurences in the string, it should actually say it returns the nnumber of non-overlapping occurences in the string

Python - Is there a way to use a greater than operator that is not a comparison operator? [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 4 months ago.
I am trying to make a program that only prints positions in a string that is greater than this position in the string, first i tried the > but it comes up with "Invalid syntax" and highlights it in red. I tried the comparison greater than but that does the same thing (Because i am not comparing)
What i have tried:
sentence = ("Mark")
print (sentence[> 1])
What i want it to print:
rk
If you have any solutions or alternatives to a greater than operator in Python please let me know and i will give it a go. :)
All you need to do is print (sentence[2:])
For future reference and research, it is called slice.

Colon (:) in Python list index [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 8 years ago.
I'm new to Python. I see : used in list indices especially when it's associated with function calls.
Python 2.7 documentation suggests that lists.append translates to a[len(a):] = [x]. Why does one need to suffix len(a) with a colon?
I understand that : is used to identify keys in dictionary.
: is the delimiter of the slice syntax to 'slice out' sub-parts in sequences , [start:end]
[1:5] is equivalent to "from 1 to 5" (5 not included)
[1:] is equivalent to "1 to end"
[len(a):] is equivalent to "from length of a to end"
Watch https://youtu.be/tKTZoB2Vjuk?t=41m40s at around 40:00 he starts explaining that.
Works with tuples and strings, too.
slicing operator. http://docs.python.org/tutorial/introduction.html#strings and scroll down a bit
a[len(a):] - This gets you the length of a to the end. It selects a range. If you reverse a[:len(a)] it will get you the beginning to whatever is len(a).

Categories