Getting value component in python? [duplicate] - python

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)]

Related

Split string into two integers, python [duplicate]

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

Convert a signed long to ASCII binarized hexidecimal format python [duplicate]

This question already has answers here:
Why does "bytes(n)" create a length n byte string instead of converting n to a binary representation?
(16 answers)
Closed 2 years ago.
Given a 8-bytes long signed value, like 3576757170468630901, I would like to convert it to ASCII binarized hexadecimal:
For example:
>> hex(3576757170468630901).encode('ascii')
b'0x31a331b2319d3175'
What I am looking for is the following format:
b'\x31\xa3\x31\xb2\x31\x9d\x31\x75'
I am not sure how can I generate it? Should I break each block and convert it myself?
In python 3 there is now to_bytes() which may help here:
v = 3576757170468630901
print(hex(v))
print(v.to_bytes(8, 'big'))
Output:
0x31a331b2319d3175
b'1\xa31\xb21\x9d1u'

Python - How do I convert number with dash to number [duplicate]

This question already has answers here:
Python: Converting string into decimal number
(8 answers)
Closed 4 years ago.
I have a number 3.8148116e-09
How do I convert it to a real number without the - ?
Thanks.
You can try:
>>> a = "3.8148116e-09"
>>> number = float(a)
>>> print "{:1.16f}".format(number)
0.0000000038148116
The first line parses the string as a number. If you need to print the number or format it for another reason, you can use string#format.

Modifying bit string in python [duplicate]

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)

Converting a string to a numerical value [duplicate]

This question already has answers here:
How to extract a floating number from a string [duplicate]
(7 answers)
Closed 8 years ago.
I wander if there is a simple way to convert a string to a number, knowing that the string begins with numbers but can contain non numerical characters.
For example: my_str = "36.12minuts"
I remember a function in Visual Basic that does the conversion directly :
my_str = "36.12minuts"
val(my_str) => 36.12
How about Python?
def digitsndots(text):
if text in ["1","2","3","4","5","6","7","8","9","0","."]:
return True
else:
return False
num = float(filter(digitsndots, "36.12minuts"))
print num
When using this make sure your string does not have digits in between like "1.a.34.c" (courtesy of #Taha)

Categories

Resources