This question already has answers here:
Single quotes vs. double quotes in Python [closed]
(19 answers)
double quotes in string representation
(1 answer)
Closed 6 years ago.
Why isn't the output of x single-quoted like y? My speculation so far is that because there's a single-quote inside x it makes it awkward if it would be printed with single-quotes. How can I prevent that from happening? Does it have to do something with escape sequences?
x = "-Why's that?"
y = "-What are you talking about dude?"
print ("%r\n%r")% (x, y)
Output:
"-Why's that?"
'-What are you talking about dude?'
Related
This question already has answers here:
How to print a number using commas as thousands separators
(30 answers)
Add commas into number string [duplicate]
(11 answers)
What's the easiest way to add commas to an integer? [duplicate]
(4 answers)
Adding thousand separator while printing a number [duplicate]
(2 answers)
Closed 2 years ago.
A beginner here! Can I ask how to split a number string in every 3rd digit from the right and put a comma in between and do it again.
>>>num = '12550'
how can I make that into this
>>>12,550
You can use this neat trick:
num = 123456789
print ("{:,.2f}".format(num))
which outputs:
123,456,789.00
If you don't want the decimal places just use:
print ("{:,}".format(num))
This question already has answers here:
replace characters not working in python [duplicate]
(3 answers)
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed 4 years ago.
I have tried to use .replace in python to replace an empty list in a string but it is not working. Could anyone please tell me how?
x = ['check-[]|man', 'check-[]|king']
for y in x:
if "[]" in y:
y.replace("[]", "o")
print(y)
The results gave me this despite using .replace:
check-[]|man
check-[]|king
y.replace returns a value.
You have to assign it back
y = y.replace("[]", "o")
You need to assign i back to variable y:
x = ['check-[]|man', 'check-[]|king']
for y in x:
if "[]" in y:
y=y.replace("[]", "o")
print(y)
Output:
check-o|man
check-o|king
This question already has answers here:
How to suppress scientific notation when printing float values?
(16 answers)
Closed 4 years ago.
How to suppress scientific notation from a float value in python. Here I tried the following code but it's not working
r_val[v].append('%.2f' % val.get("closing_balance"))
Thanks in advance
Using format(x, '.#f')
consider this snippet:
x = 0.000000235
print(x)
2.35e-07
print (format(x, '.9f'))
0.000000235
Or, to go closer to your question:
y = -1.06267582739e-11 # note I changed '+' to '-' since '+' is is just represented as a regular float
print(y)
-1.06267582739e-11
print(format(y,'.22f'))
-0.0000000000106267582739
This question already has answers here:
How to use digit separators for Python integer literals?
(4 answers)
What do 1_000 and 100_000 mean? [duplicate]
(2 answers)
Closed 4 years ago.
I find a weird thing in Python, if I assign a variable as int_int, float_int, int_float, the type will be int, float, float, seems the underline is a join character, like:
x = 100_200
then x will be a int, value is 100200. Can someone explain why and how to reasonable use?
Python 3.6 introduced Underscores in Numeric Literals.
Essentially, this is a readability feature.
Compare:
x = 100000000
and:
x = 100_000_000
This question already has answers here:
Convert binary to ASCII and vice versa
(8 answers)
Closed 6 years ago.
x = "01100001"
How do i convert this string into ASCII
Expected Result:
print(x.somefunction())
Output: a
Use following one liner
x = chr(int('010101010',2))
will work.