How to add Commas in Python? [duplicate] - python

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

Related

I have a string as '11-2020', I want to convert it as 'NOV-2020' in Python [duplicate]

This question already has answers here:
How to change a string into uppercase?
(8 answers)
Closed 2 years ago.
What is the best pythonic way to covert, a string as '11-2020' to 'NOV-2020' in Python.
I tried below code :
print(datetime.datetime.strptime(date, '%m-%Y').strftime('%b-%Y'))
But getting output like : Nov-2020 (I want Nov to be in caps)
print(datetime.datetime.strptime(date, '%m-%Y').strftime('%b-%Y').upper())
#NOV-2020

How to change 000-000 to 000000 in Python? [duplicate]

This question already has answers here:
How to delete a character from a string using Python
(17 answers)
Closed 4 years ago.
I'm making an odoo10 module, but I need to change a string like
000-000
to
000000
How can I do that?
print("000-000".replace("-", "")) #Output: "000000"
Try this
Oldstr = “000-000”
Newstr = Oldstr.replace(“-”,””)
That should do it.
What I did:
Make the string “000-000”
Made a new string and replaced the “- ”in the old one with nothing and saved it in a new variable

Python convert Hexadecimal Character to Respective Symbols? [duplicate]

This question already has answers here:
How do I url unencode in Python?
(3 answers)
Closed 5 years ago.
I'm trying to find a python package/sample code that can convert the following input "why+don%27t+you+want+to+talk+to+me" to "why+don't+you+want+to+talk+to+me".
Converting the Hex codes like %27 to ' respectively. I can hardcode the who hex character set and then swap them with their symbols. However, I want a simple and scalable solution.
Thanks for helping
You can use urllib's unquote function.
import urllib.parse
urllib.parse.unquote('why+don%27t+you+want+to+talk+to+me')

Regex for all kind of number variations [duplicate]

This question already has answers here:
How to extract a floating number from a string [duplicate]
(7 answers)
How to extract numbers from a string in Python?
(19 answers)
Closed 6 years ago.
I am using python to parse some strings that contain numbers and I want to find a regex that will extract all kind of scenarios:
.2345 0.934 12.3 11.0
Tried something like:
((\-|\+)?[0-9]+(\.[0-9]+)?)
But it seems like cases with .number are not covered.
Your RegEx is correct, but you want to parse numbers which starts with . also, so you can add \. along with \-|\+ as follow:
((\-|\+)?(\.)?[0-9]+(\.[0-9]+)?)
Note: It will match .1.1

Why is this split method not working correctly? [duplicate]

This question already has answers here:
why is python string split() not splitting
(3 answers)
Closed 6 years ago.
I'm trying to split
<team>
into just team, here is the code I'm using:
s = "<team>"
s.split(">")[1]
s
'<team>'
s.split(">")[1].split("<")[0]
s
'<team>
As you can see, it's still leaving me with
<team>
anyone know why>
str.split() function returns a list, it does not split the string in place.
You'll need to make a new variable:
s = "<team>"
t = s.split(">")[1]
t

Categories

Resources