This question already has answers here:
Why does reversing a list using slice notation with 0 as "stop" not return the entire list?
(9 answers)
How do I reverse a string in Python?
(19 answers)
Closed 10 days ago.
I am trying to reverse a string in python but i cannot include the first letter.
I tried this code:
a = "Helloworld"
print(a[3:0:-1])
but it doesn't work.
I also tried:
a = "Helloworld"
print(a[3:-1:-1])
It displays nothing when i try this.
The code you tried doesn't work because the slice a[3:0:-1] starts at index 3 and goes all the way to index 0 (in reverse), but it includes index 0, which is the first letter of the string.
The slice a[3:-1:-1] starts at index 3 and goes to the index before the last one (-1), but in reverse. This would give an empty string because the step value of -1 goes in the opposite direction of the start and end indices.
To reverse the string excluding the first letter, you can slice it like this:
a = "Helloworld"
print(a[1:][::-1])
Related
This question already has answers here:
How do I reverse a string in Python?
(19 answers)
Closed 1 year ago.
I have a code in which I am attempting to reverse a certain string. I have received instructions to only use index to do this, so I used index, and came to a halt when I found this. Here is my code first, for context.
emptyList = []
Len = len(var)
for i in range(Len, 0, -1):
emptyList.append(i)
print(emptyList)
My problem is that whenever I print the variable emptyList, I only get a list of numbers, like such:
[3, 2, 1]
I want the numbers to represent their character, like 3 = G, 2 = O. How do I do that?
Use emptyList.append(var[i-1]) not just i.
This question already has answers here:
Writing a function that alternates plus and minus signs between list indices
(7 answers)
Closed 2 years ago.
Given a list of integers, I want to subtract all the integers with an odd index and add all the integers with an even index. Is there any compressed way to do this without the regular "while loop with iterator and add/subtract"?
Use slicing with a step:
n = sum(L[0::2]) - sum(L[1::2])
The sequence slicing syntax L[i:j:k] is documented here, specifically refer to the note 5.
This question already has answers here:
How to delete the very last character from every string in a list of strings
(5 answers)
Closed 5 years ago.
Suppose,
a = ["Power is nothing"]
How to remove last string in variable 'a' which is 'g'?
You can use list slicing:
a = a[0][:-1]
a[0] selects the first element in your list. in your case, a[0] is "Power is nothing". Then [:-1] returns a new string without the last letter.
You can check the documentation for any detail regarding list slicing in python
This question already has answers here:
'str' object does not support item assignment [duplicate]
(10 answers)
Closed 8 years ago.
So I have to create a function who take a list like this:
[d,o,g] --> each caracter has a position
and the return is the reverse word...
So far I have this:
def invertido(x):
largo= len(x)
for i in range (0, largo/2):
x[i] = x[largo -i]
print x
I have the following error: TypeError: 'str' object does not support item assignment
You can do this directly using the indexing syntax in Python. Try:
word_inverted = word[-1::-1]
This syntax means "start at the last letter in word (index -1), move backwards one letter at a time (the -1 at the end), and move through all of the letters in the word (the :: in the middle)"
In general, you can index an array (a string is just an array of characters) with the syntax array[first:last:step], where first is the first item that you want, last is the first item that you don't want (i.e. the last item you get is the item right before last) and step is how far you want to move for each successive item.
You can also use some shortcuts to just grab all letters from the beginning of the word to last with array[:last:step], all letters from first to the end with array[first::step], and all letters at some interval step with array[::step]. Lastly, if you enter a negative value for step, then you step backwards through the array.
This question already has answers here:
How to find the last occurrence of an item in a Python list
(15 answers)
Closed 8 years ago.
For example, I have a list
[0,2,2,3,2,1]
I want to find the index of the last '2' that appears in this list.
Is there an easy way to do this?
You can try the following approach. First reverse the list, get the index using L.index().
Since you reversed the list, you are getting an index that corresponds to the reversed, so to "convert" it to the respective index in the original list, you will have to substract 1 and the index from the length of the list.
n = ...
print len(L) - L[::-1].index(n) - 1