Convert 11/2/1998 to 110298 [duplicate] - python

This question already has answers here:
how to format a date in shell script
(3 answers)
Closed 2 years ago.
What is the simplest way to convert a date formatted 3/2/2004 to 030204 in pure bash, or if not possible, then Python?
I need to at zeros in front of an single digit sectors of the date, remove the parenthesis, and have only the last two characters of the 4 digit year.
I know I could write an extensive Python script that would create an array splitting at /, and for any single digit arrays I would add a 0. I don't want to do this because it seems unnecessary. Thanks in advance for any help!

date -d'11/2/1998' +%m%d%y
110298

import datetime
d = datetime.datetime.strptime("11/2/1998", "%d/%m/%Y")
print d.strftime("%d%m%y")

Related

convert string to number without 'replace' Python [duplicate]

This question already has answers here:
Convert decimal mark when reading numbers as input
(8 answers)
Closed 10 months ago.
I take the current exchange rate from the bank's website as a string and I want to convert this string into a number for further calculations and I want to do it as beautifully as possible.
How to convert the string 77,4651 $ to 77.4651 in float format without using func 'replace'?
Use float regular expression to make sure, that you get the float number
txt = "77.4651 $"
x = float(re.search("[-+]?[0-9]*(?:\.?[0-9]*)[1]", txt).string)
or less safe split by spaces
float("77.4651 $".split("\s+")[0])
Regex would be useful here to account for varying formats:
import re
float('.'.join(re.findall('[0-9]+', "77,4651 $")))

Use Python 3 str.format to truncate a float to have no more than x number of digits [duplicate]

This question already has answers here:
Formatting floats without trailing zeros
(21 answers)
Closed 4 years ago.
I am looking for an expression that will truncate a float to at most a certain number of digits. I want to preserve a certain number of decimals, without having unnecessary trailing 0s.
So, this almost works as desired:
"{0:3.f"}.format(number)
For input 3.123000001:
"{0:.3f}".format(3.1230000001)
'3.123'
Great. But for input 3:
"{0:.3f}".format(3)
'3.000'
I figured out the answer while I was writing the question. Just add .rstrip('0') to the expression. So:
"{0:3.f}".format(number).rstrip('0')

How can i print '\' in python? [duplicate]

This question already has answers here:
How can I print a single backslash?
(4 answers)
Closed 4 years ago.
Is there any way to print back slash in python? we can write a string in three format.
1. ASCII
2. Unicode
3. Raw String
I have tried with all 3 formats but not able to get expected result.
Thanks in Advance
Use double backslash, first one marks the escape character:
print("\\")
First option - Unicode:
print('\u005c')
Second option:
print('\\')

How to add Commas in Python? [duplicate]

This question already has answers here:
Add commas into number string [duplicate]
(11 answers)
Closed 6 years ago.
I have this code finished and it runs fine, i just cannot figure out how to add commas into each dollar value.]
Here are pics of the code: Pic 1
Pic 2
Any help would be great,
Thanks.
You'll need to format the float numbers yourself.
Or use babel.numbers.format_currency, from the Babel library. See: http://babel.pocoo.org/en/latest/api/numbers.html
Example:
>>> format_currency(1099.98, 'USD', locale='en_US')
u'$1,099.98'
You can also use this format:
print ("${:,.2f}".format(1234.56))
# -> $1,234.56

Python - Remove first three chars' of string [duplicate]

This question already has answers here:
Are there limits to using string.lstrip() in python? [duplicate]
(3 answers)
Closed 8 years ago.
So I have a super long string composed of integers and I am trying to extract and remove the first three numbers in the string, and I have been using the lstrip method (the idea is kinda like pop) but sometimes it would remove more than three.
x="49008410..."
x.lstrip(x[0:3])
"8410..."
I was hoping it would just remove 490 and return 08410 but it's being stubborn -_- .
Also I am running Python 2.7 on Windows... And don't ask why the integers are strings. If that bothers you, just replace them with letters. Same thing! LOL
Instead of remove the first 3 numbers, get all numbers behind the third position. You can do it using : operator.
x="49008410..."
x[3:]
>> "8410..."

Categories

Resources