how to extract digits from a string? [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
i have a list
a=['invoice β invoice # 2018-33167-2 β date 03/21/2018 β total due $8,804.90']
i want to extract only digits from this list of the form
a=['2018-33167-2 03/21/2018 8,804.90']
i have used regex to extract
for i in a:
res = re.sub("\D", "", i)
but the result is
res=201833167203212018880490

Based on the expected output, you want more than just digits. The digits can have /, - and ,.
Maybe, perhaps, you wanted something that starts with and ends with a digit, but anything in the middle other than a space?
import re
a='invoice β invoice # 2018-33167-2 β date 03/21/2018 β total due $8,804.90'
print(re.findall(r'\d[^ ]+\d', a))
output
python3 test.py
['2018-33167-2', '03/21/2018', '8,804.90']

Related

I am trying to convert a string from a list into an integer without losing the decimal places [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
I want to convert a string to integer without rounding. For example
s = "99.7"
x = s(int(float(s))
Output:
99
But I want the output to be 99.7
I was thinking of just adding all the strings to a list and somehow converting the list to an integer but I am not sure how to do that or how to even do it individually.
Desired output:
x = '99.7'
z = int(x)
output:
99.7
An integer in python can not have a floating point. To show this you should use
float(x)
This will prevent any rounding.

perform arithmetic operations python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
given the number 12345 = 1x10^4 + 2x10^3 + 3x10^2 + 4x10^1 + 5x10^0, how to perform some arithmetic operations to leave you with just the digit at position 5 (from the left) and output it to the screen? Thanks for the help!
Assuming you want to stick to arithmetic operations (and not strings), use the modulo operator with 10 to get the remainder of division by 10, i.e. the unit:
12345%10
output: 5
For an arbitrary number, you need to compute the position, you can use log10 and ceil:
from math import log10, ceil
N = 5
number = 1234567
number//10**(ceil(log10(number))-N)%10
output: 5

How are escape sequences contributing to the length of a string? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
a=("\nHello")
print(len(a))
The intended output is a new line with 5 printed but instead there is no new line and the length is coming out to be 6.
There is a new line printed, plus the 5 characters in 'Hello', that's 6 in total:
a = "\nHello"
print(a)
print("Total length is", len(a))
Output
<space>
Hello
Total length is 6

Format String - number [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
This is a easy question but i need explanation.
In this code Why the output is 100:
x = 100.1205
y = str(x)[6]
print("{:.{}f}".format(x,y))
x = 100.1205
y = str(x)[6] ## This is turning x into a string and grabbing the 6th character which is 0
print("{:.{}f}".format(x,y)) # this is saying print x to the decimal place
#specified by the value of y (zeroth digit) which is 100

Python integer spacing [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a code like this.
print (' ', totalh, totalt, totalo)
and I want the integer of totalh to be 2 2 instead of 22 with a space in-between the integers. How do I go about doing it?
Convert it to a string which is an iterable and can be an argument to " ".join.
totalh = 22
print(" ".join(str(totalh)))
Output:
2 2
I suggest you this simple way: first convert totalh into a string with each digit being a character, then use " ".join() to insert a space between each digit:
totalh = 22
print(" ".join(str(totalh))) #2 2

Categories

Resources