This is what I have, but I know it is incorrect and I'm not sure what to change
print '0.4066145E-07-0.3677403'.split('E+(\-\d{2})', 1 )
I'm looking to get:
['0.4066145E-07','-0.3677403']
or more generally I just want to split up these numbers.
['######E-##','#########']
Also what if there is an exponent in the second number?
['######E-##','#######E-##']
You can try with:
(?<=E-\d\d)(?=-\d+.)
DEMO
Related
I am trying to put comma's on a number without striping any numbers after the decimal point.
number
12018093.1000
results
12,018,093.10
I tried this code but it strips away the last 0 which I don't know why.
rps_amount_f = ("{:,}".format(float(rps_amount_f)))
I'm not sure why the zeros are stripped away in your example, although this may be a solution using an f-string:
rps_amount_f = 12018093.1
rps_amount_f_str = f"{rps_amount_f:,.10f}"
Here '10' before the f is the decimal precision you want to have in the string, i.e. 10 decimals in this case.
You said that you only want one zero so really isn't the solution just to add a 0 to the end of the string? Anyway here's my solution:
if("." in str(rps_amount_f)):
rps_amount_f = ("{:,}".format(float(rps_amount_f)) + "0")
else:
rps_amount_f = ("{:,}".format(float(rps_amount_f)))
If you want two decimal places you just get rid of the if statement and round it.
print("{:,.4f}".format(float(rps_amount_f))) # 12,018,093.1000
https://docs.python.org/3/library/string.html#format-specification-mini-language
I want to know if there is any way to differentiate a whole number from any other output only using maths, eg if you have the number 5 I would like to convert that into the number 0 using equations, however, the number 5.4342 would output the number -1
(What i am trying to do is very hard to put into words so i can clear up any questions)
Use type() to check if it's an integer or a float. In python, it's usually best to do it this way.
Mathematically, if you allow integer divisions, you could use this:
def isInt(n): return 0**(n-n//1)-1
isInt(5) # 0
isInt(5.4342) # -1.0
If can also work with a modulo:
def isInt(n): return 0**(n%1)-1
x=raw_input('what is your favorite number? ')
n=x*10
print n
If I plug in 5, I don't get 50. I get 5555555
I have tried declaring float(n) and tooling around with it. nothing helped. I realize this is minor league, but I am just starting to teach myself python.
Thanks, Todd
You are taking in your number as raw_input, meaning, it is returned to the program as a string. When you multiply a string by an integer and print the result, it just prints the string x times, in your case 10 because you attempted to multiply by 10. To prove this, change, the 10 to 20 and watch what happens.
There are two ways to fix this. The first would be to use input() instead of raw_input(), so that it returns a number as a result.
x=input("Please enter a number here.\n")
The second way would be to reassign x to the integer equivalent of the string, using the function int().
x=int(x) # Will turn "10", which is what you have, into 10
This should solve your problem.
Best of luck, and happy coding!
This is because the default data type of raw_input() is string. You have to cast the string input to an integer for achieving the desired result.
When you read a value in from user input like this it is as a string. So x actually equals the string '5' but what you actually want is the number 5.
int(x) * 10
i have a code where the out put should be like this:
hello 3454
nice 222
bye 45433
well 3424
the alignment and right justification is giving me problems.
i tried this in my string {0:>7} but then only the numbers with the specific amount of digits are alright. the other numbers that have some digits more or less become messed up. it is very obvious to understand why they are messing up, but i am having trouble finding a solution. i would hate to use constant and if statements all over the place only for such a minor issue. any ideas?
You could try:
"{:>10d}".format(n) where n is an int to pad-left numbers and
"{:>10s}".format(s), where s is a string to pad-left strings
Edit: choosing 10 is arbitrary.. I would suggest first determining the max length.
But I'm not sure this is what you want..
Anyways, this link contains some info on string formatting:
String formatting
You can try this:
def align(word, number):
return "{:<10s}{:>10d}".format(word, number)
This will pad-right your string with 10 spaces and pad-left your number with 10 spaces, giving the desired result
Example:
align('Hello', 3454)
align('nice', 222)
align('bye', 45433)
align('well', 3424)
I want to generate numbers from 00000 to 99999.
with
number=randint(0,99999)
I only generate values without leading zero's, of course, a 23 instead of a 00023.
Is there a trick to generate always 5 digit-values in the sense of %05d or do I really need to play a python-string-trick to fill the missing 0s at front in case len() < 5?
Thanks for reading and helping,
B
You will have to do a python-string-trick since an integer, per se, does not have leading zeroes
number="%05d" % randint(0,99999)
The numbers generated by randint are integers. Integers are integers and will be printed without leading zeroes.
If you want a string representation, which can have leading zeroes, try:
str(randint(0, 99999)).rjust(5, "0")
Alternatively, str(randint(0, 99999)).zfill(5), which provides slightly better performance than string formatting (20%) and str.rjust (1%).
randint generates integers. Those are simple numbers without any inherent visual representation. The leading zeros would only be visible if you create strings from those numbers (and thus another representation).
Thus, you you have to use a strung function to have leading zeros (and have to deal with those strings later on). E.g. it's not possible to do any calculations afterwards. To create these strings you can do something like
number = "%05d" % random.randint(0,99999)
The gist of all that is that an integer is not the same as a string, even if they look similar.
>>> '12345' == 12345
False
For python, you're generating a bunch of numbers, only when you print it / display it is it converted to string and thus, it can have padding.
You can as well store your number as a formatted string:
number="%05d" % random.randint(0,9999)