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).
Related
This question already has answers here:
str.startswith with a list of strings to test for
(3 answers)
Closed 1 year ago.
I've tried using the or function to input multiple words for the same output, but it only takes the first word as the input and not the rest. How do I solve this? Thanks!
For instance:
message.content.startswith("hi" or "hey")
only takes in "hi" as an input and not "hey".
I've tried adding the words in to a list and it doesn't work as well. I'm relatively new to coding so i'm sorry in advance if it's a stupid question
You can code like this:
message.content.startswith(("hi", "hey"))
From the Python documentation for str.startswith(prefix[, start[, end]]), I've added emphasis:
Return True if string starts with the prefix, otherwise return
False. prefix can also be a tuple of prefixes to look for. With
optional start, test string beginning at that position. With optional
end, stop comparing string at that position.
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.
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.
This question already has answers here:
Understanding slicing
(38 answers)
Closed 9 years ago.
thanks to answers on this site I now know that you can remove the last characters of a string by string[:-1] which was really helpfull, however I need to be able to remove the first aswell and as far as I understand this technique it is not possible. so are there other ways to remove parts of strings without replacing spesific letters?
What do you mean "it is not possible"? :)
It is perfectly possible with Explain Python's slice notation:
>>> mystr = 'abcde'
>>> mystr[1:] # Remove the first
'bcde'
>>> mystr[1:-1] # Remove the first and the last
'bcd'
>>> mystr[2:-2] # Remove the first two and the last two
'c'
>>>
string[1:]
You may need to read some documentation. :)
This question already has answers here:
How do I get a substring of a string in Python? [duplicate]
(16 answers)
Closed 9 years ago.
I have a list of strings in the format: 'foo7bar'. How is it possible in Python to remove the 7 along with any characters that follow?
This is similar to this question, but I need the answer for Python.
You can do this using Python's slice notation:
>>> mystr = 'foo7bar'
>>> mystr[:mystr.index('7')]
'foo'
>>>
The format for slice notation is [start:stop:step]. The index method of a string finds the position of the first occurrence of its input.
Note however that if you are dealing with something more complex (such as matching patterns), you might want to look into Regular Expressions. For this operation though, the slice notation is sufficient.