This question already has answers here:
Understanding slicing
(38 answers)
Closed 2 years ago.
So I have this code:
t=int(input())
while t:
s=int(input())
n=bin(s)
n=n[2:][::-1]
if n.count('1')==1:
pos=n.find('1')+1
print(pos)
else:
print('-1')
t-=1
I would like to know exactly what's going on in this line:
n=n[2:][::-1]
What does [::-1] means?
It takes the reverse of binary value of n excluding "0b" values in the beginning. For example if you enter 6 for value of n. The binary value would be 0b110 and reverse value excluding 0b would be 011.
Related
This question already has answers here:
Understanding slicing
(38 answers)
Closed 1 year ago.
Here as we can see in the image when we slice the string from 0:len(s) we are getting the full string, but when I try to print s of len (s) instead of getting the last character it throws me n error.
I'm new to python so have some mercy! thanks!!
s='hello'
print(s[0:len(s)])
print(s[len(s)])
The string length is always 1 more than the max index since index is starting from 0 for single character while length starts with 1 for single character.
Please change your code to be like this:
s='hello'
print(s[0:len(s)-1])
print(s[len(s)-1])
This question already has answers here:
Given n, take tsum of the digits of n. If that value has more than one digit, continue reducing a single-digit number is produced
(4 answers)
Closed 1 year ago.
I have problem and trying to get next:
new_string = "35" #and this result must be like new_int = 3+5.
How im available to do this? I know the type conversion, but not a clue how i should do this.
As you are new to the python, i suggest you doing it using
int(new_string[0]) # 3
int(new_string[1]) # 5
So now you have 2 integers, you can to whatever you want
This question already has answers here:
How to extract individual digits from a list of integers?
(3 answers)
How to find length of digits in an integer?
(31 answers)
Split an integer into digits to compute an ISBN checksum [duplicate]
(15 answers)
Closed 2 years ago.
I try to run the code:
for c in range(10, 21):
print(c)
print([c[1]])
And I get the error:
TypeError: 'int' object is not callable
Why? How can I pick a specific element from an integer?
you are iterating in an iterable, you need to declare before the list to access to the element something like this:
a = range(10,21)
for i in a:
print(i)
print(a[1])
In your code every iteration the value of c are:
10
11
12
13
...
This is the reason of you error
This question already has answers here:
Understanding slicing
(38 answers)
Closed 2 years ago.
So, I have a string, for example:
string = '34567'
I know that string[-1] will return me '7', but how do I get like '567'? Because it doesn't work if I start from the middle:
string[2:-1] == '56'
Usually, since the last number doesn't count, you just add 1 to it, but in this case you can't do it
So how do I print '567'?
but in this case you can't do it
You can by not providing it.
>>> '34567'[-3:]
'567'
Which basically asks for "the last 3 characters of the string"
To get the last 3 characters of a string you slice this way:
string[-3:]
This question already has answers here:
How to pad a string with leading zeros in Python 3 [duplicate]
(5 answers)
Closed 5 years ago.
For any k-digit integer i, the goal is to produce an m-digit string where the first n digits, where n=m-k are zeros, say. Using python would be helpful.
For example, given m=5 and i=324, how to produce "00324"?
EDIT:
The zfill function pads the integer with zeros. Is there any more general function that pads the integer with an arbitrary integer/character?
You can use zfill
i = 324
m = 5
s = str(i).zfill(m)
# '00324'