This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
reverse a string in Python
I'm trying to understand how to reverse the letters in a string. Let's say that I have hello and am looking for the output olleh how would I implement this using the list as a tool?
Using slice notation,
forwards = "hello"
backwards = forwards[::-1]
(The third section of slice notation is the step; in this case, -1 makes it step backwards through the entirety of the string, effectively reversing it.)
or, using the reversed() function:
backwards = ''.join(reversed(forwards))
(Note that without the ''.join(), you'd get a <reversed object at 0x1215a10> instead.)
>>> print backwards
olleh
With slice notation:
string = "Hello!"
reversed_string = string[::-1]
Related
This question already has answers here:
Understanding slicing
(38 answers)
Closed 3 years ago.
Below is the code.
I don't understand the meaning of the last bracket in [::-1][:-1], and how come can you write two brackets at the same time. I understand that the first slice bracket reverses the order of the string, but what does the second do?
for i in range(n,0,-1):
temp = list(alphabets[:n][i-1:])
print('-'.join(temp[::-1][:-1]+temp).center(4*n-3,'-'))
Thanks for cooperation!
To answer your question I will use an example:
list = [1,2,3,4,5]
As you said, the first brackets will reverse your list.
Then, you will get as a result: [5,4,3,2,1]
On that list you will do the slicing: [:-1]. That will give you as a result [5,4,3,2].
The brackets meaning is the same:
[<startIndex>:<endIndex>:<how to go through>]
For more information about the <how to go through> part, please read here: https://www.pythoncentral.io/how-to-slice-listsarrays-and-tuples-in-python/
This question already has answers here:
Understanding slicing
(38 answers)
Closed 3 years ago.
If I have a string of variable length and I want to return only first 3 characters of the string.
str[:3]
this works but I want to know if the string is of lesser length, suppose 2, "ab" or just "a", will this slicing work for that too?
Thanks in advance
Python will return at most :n characters:
'a'[:3] will simply return 'a'. ''[:3] returns ''.
You could have tested for yourself in less time than it would have taken to open your browser.
But yes.
This question already has answers here:
How to create a "singleton" tuple with only one element
(4 answers)
Closed 3 years ago.
Why type((1)) is int and not a tuple? Whereas type((1,)) gives tuple.
That's also an answer to the question why we should use commas while defining a tuple with one value. Because tuples are not like lists which is unique in a way that we define it (using squared brackets) we have to add the comma to the value. In the first one type((1)) inner paranthesis have no effect, so it's just a basic integer nothing else. Like when you define expressions in paranthesis to give them priority. Hope it helps :)
Python compiler treated (1) as 1 because of that it is showing as int. that is inbuilt behavior of python compiler.
>>> a = (1)
>>> print(a)
1
>>> a = (1,)
>>> print(a)
(1,)
This question already has answers here:
Count overlapping substring in a string [duplicate]
(6 answers)
Closed 6 years ago.
I am new to python and learning. As given here count() method when used on strings gives the number of occurrences of sub string in a string.
So when i Do :
'BANANA'.count('ANA')
Expected output should be 2 as 'ANA' occurs twice in 'BANANA' but count returns 1.
Can someone please explain this, or maybe i have misunderstood something.
Please point me in the right direction.
>>> help(str.count)
Help on method_descriptor:
count(...)
S.count(sub[, start[, end]]) -> int
Return the number of non-overlapping occurrences of substring sub in
string S[start:end]. Optional arguments start and end are
interpreted as in slice notation.
Notice the non-overlapping.
You can use regular expressions to find it. Use the function findall from module re to find overlapping occurences
import re
len(re.findall('(?=ANA)', 'BANANA'))
which yields 2.
Or yields 3 here:
import re
len(re.findall('(?=ANA)', 'BANANAANA'))
This question already has answers here:
Understanding slicing
(38 answers)
Closed 9 years ago.
Please explain me what does this piece of code do.
h should be 32Byte result from sha256 calculation.
I am rewriting parts of this code for my project in C++ and I'm not sure if this switches byte order per 4byte chunk or change byte order on whole 32byte number.
def reverse_hash(h):
return struct.pack('>IIIIIIII', *struct.unpack('>IIIIIIII', h)[::-1])[::-1]
And, how does this array index work ?
[::-1]
Thanks for any and all info
[::-1] creates a new list with reversed order of elements
[::-1] reverse order of elements in a list, so this script change order of each 4-bytes subsequence (in order to change endiannes, I suppose):
>>> h = ''.join(map(str, range(0,21)))
>>> h
'01234567891011121314151617181920'
>>> struct.pack('>IIIIIIII', *struct.unpack('>IIIIIIII', h)[::-1])[::-1]
'32107654019821114131615181710291'
Equivalent expression:
>>> struct.pack('<IIIIIIII', *struct.unpack('>IIIIIIII', h))
'32107654019821114131615181710291'