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:]
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:
Python - Split integer into individual digits (undetermined amount of digits)
(4 answers)
Closed 1 year ago.
I cannot describe clearly because English is not my native language
If I input like this
a = 4252
Then I want to take each number component, it should be like this
a1=4; a2=2; a3=5; a4=2
How to do that in python?
If you know it has a similar question in stackoverflow, give me a link
Convert the integer to a string for easy iterability then get the int value of every character like this: a1,a2,a3,a4 = [int(elem) for elem in str(a)]
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.
This question already has answers here:
Replace first occurrence of string in Python
(2 answers)
Closed 4 years ago.
Currently, i have a bit string represented as
current = "011"
and what I'm trying to do is to create a new string based of the bit string above with the 1 at index 1 replaced with 011 which would give me:
new = "00111"
The problem I'm having is that when I use the replace function, it replaced all the 1 in the string including the one at index 2 which is not what I desired.
new = current.replace("1","011")
= 0011011 #not what I wanted
Would appreciate some help on this.
Limit the number of replace to 1 such as below:
new = current.replace("1","011", 1)