Adding comma after a character in string [duplicate] - python

This question already has answers here:
How to add a string in a certain position?
(10 answers)
Closed 7 months ago.
number = input("Enter the number:")
''' Let's suppose the entered number is 0145. Question is below.'''
I want to add a comma after 0.
How can i do this?

Use {:,} to format a number with commas in Python.
Example:
number = 1000
print(f"{number:,}")
Output:
 
1,000
If want a general purpose number formatter for numbers as strings that may include leading 0's then there is a solution using regular expressions here.
If user enters "0145" and want to format that as "0,145" then you'd need to use the string input rather than converting to an integer which would drop any leading 0's.

Related

Take integer from input with string and integer [duplicate]

This question already has answers here:
How to extract numbers from a string in Python?
(19 answers)
Closed 1 year ago.
I want to figure out how to take the integer from a input when there is both a integer and string.
For example, if I input "hello 3", is there a way I can separate the "3" to another variable aside from the "hello"?
Would this work for you:
myInput=input() # Get Input
myString,myIntStr=myInput.split(" ") # Split in to list based on where the spaces are
myInt=int(myIntStr) # Convert to int

Calculating an adition presented as a string [duplicate]

This question already has answers here:
Evaluating a mathematical expression in a string
(14 answers)
Safely evaluate simple string equation
(4 answers)
Closed 2 years ago.
Say you have:
text = "22 + 33"
And you want to verify that the sign used it is actually an addition sign:
for word in text:
If word == "+":
How do I find the index/position of the addition sign inside the string?
How do you find the index/position of the numbers to the sides of the addition sign?
I want to do this to then assign these values to variables and then be able to calculate the problem.
PD: The problem is that, the string is not only going to have the addition and thats it, it will contain much more words and signs. Plus, it will be constantly updating (every 0.5 secs). Thats why I want to locate if there is an ADDITION sign and then find the two numbers to its sides to consecuently calculate the sum. Therefore, following my approach, I should need the indexes of the the three things; number1, addition sign and number 2.
You can use the eval() function in Python to evaluate an expression.
For example, consider the following code:
text = "22 + 33"
print(eval(text))
This will print out 55, which is 22 + 33

How to split a number string using a comma in every 3rd digit from the right [duplicate]

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

How to format '7' to produce '007' [duplicate]

This question already has answers here:
How to pad a string with leading zeros in Python 3 [duplicate]
(5 answers)
Closed 2 years ago.
How would you format '7' to print '007' here:
>>> number = 7
>>> print(number)
7
Ideally, there'd something similar to always showing these decimal places?:
>>> number = 7
>>> print("{:.2f}".format(number))
7.00
I've searched online for a similar solution to this but can't seem to find anything in this format...
Thanks for the help!
You just need to cast 7 using str() and finally use zfill to add leading zeros:
Return a copy of the string left filled with ASCII '0' digits to make
a string of length width. A leading sign prefix ('+'/'-') is handled
by inserting the padding after the sign character rather than before.
The original string is returned if width is less than or equal to
len(s).
str(7).zfill(3)
>>> '007'
try this
print("{0:0=3d}".format(number))

What is the type of a number with underscores separating its digits? [duplicate]

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)
Declaring a number in Python. Possible to emphasize thousand?
(2 answers)
Closed 4 years ago.
I am wondering why the following variable is treated like a number?
a = 1_000_000
print (a)
1000000
Shouldn't print(a) return 1_000_000?
With Python 3.6 (and PEP-515) there is a new convenience notation for big numbers introduced which allows you to divide groups of digits in the number literal so that it is easier to read them.
Examples of use:
a = 1_00_00 # you do not need to group digits by 3!
b = 0xbad_c0ffee # you can make fun with hex digit notation
c = 0b0101_01010101010_0100 # works with binary notation
f = 1_000_00.0
print(a,b,c,f)
10000
50159747054
174756
100000.0
print(int('1_000_000'))
print(int('0xbad_c0ffee', 16))
print(int('0b0101_01010101010_0100',2))
print(float('1_000_00.0'))
1000000
50159747054
174756
100000.0
A = 1__000 # SyntaxError: invalid token
Python allows you to put underscores in numbers for convenience. They're used to separate groups of numbers, much like commas do in non-programming. Underscores are completely ignored in numbers, much like comments. So this:
x = 1_000_000
is interpreted to be the same as this:
x = 1000000
However, you can't put two underscores right next to each other like this:
x = 1__000__000 #SyntaxError
In English speaking countries, commas are generally used as thousand separators, while in many other countries, periods are used as thousand separators. Given the differing conventions, and the fact that both commas and periods are used for other things in Python, it was decided to use underscores as separators.

Categories