Python Forum

Full Version: extracting from a string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Dear All

I have run into an anomaly. This snippet of code works
original_string = "Hello, World!"
extract = original_string[7:12]  # Extracts "World" 
print(extract)       # Output: World 
When I expand the code to do a little more, the above code works, but the second occurrence of [2:2] does not
Here is the whole file
original_string = "Hello, World!"
extract = original_string[7:12]  # Extracts "World" 
print(extract)        # Output: World 

total = float(input("Enter total bill: "))    # Enter 173.59
print("Thank you")
people = int(input("Enter the number of people: ")) # Enter 9
each = str(total/people)
print("String =", each)     # output: String = 19.287777777777777
extract2 = each[2:2]
print("[", extract2, "]") # output: [  ]
The purpose is to extract the first 2 decimal places and not to round up. I want to preserve the first 2 decimal places.
round and format, both round up
print("Each person will pay", round(total/people, 2))     # output 19.29
print("Each person will pay", format(total/people, '.2f'))   # output 19.29
Any help appreciated
total = 173.59
people = 9
each = total/people
print(f"String = {each}")
extract2 = str(each)[3:5]
print(f"[{extract2}]")

print(f"Each person will pay {each:.2f}")
Output:
String = 19.287777777777777 [28] Each person will pay 19.29
Thanks Axel

This is a new approach. I have learnt something.

Regret not the solution I was looking for. Your solution still rounds up.

"The purpose is to extract the first 2 decimal places and not to round up."

Any further thoughts welcome
Ok , is this better?

total = 173.59
people = 9
each = total/people
print(f"String = {each}")
extract2 = f"{each:.2f}".split(".")[1]
print(f"[{extract2}]")

print(f"Each person will pay {each:.2f}")
Output:
String = 19.287777777777777 [29] Each person will pay 19.29
You don't want to round. You want to truncate.
import math

for x in (4.567, -4.567, 20 / 3):
    print(f"{x} {x:.3} {int(x*100)/100} {math.trunc(x * 100)/100}")
Output:
4.567 4.57 4.56 4.56 -4.567 -4.57 -4.56 -4.56 6.666666666666667 6.67 6.66 6.66
If you really want to use slices.
for x in (4.567, -4.567, 20 / 3):
    numstr = f"{x:0.3f}"[:-1]
    print(x, numstr)
Output:
4.567 4.56 -4.567 -4.56 6.666666666666667 6.66
Quote:"The purpose is to extract the first 2 decimal places and not to round up."

Just work on the string of the number? The waiter comes:

“Here is your bill Sir."

bill1 = '£173.59'
# assume 9 people
each1 = float(bill1[1:])/9 # 19.287777777777777
s1 = str(each1)
all_pay = '£' + s2[0:s2.find('.')] + '.' + s2[s2.find('.') + 1:s2.find('.') + 3]
Output:
'£19.28'
bill2 = '£17235.59'
each2 = float(bill2[1:])/9 # 1915.0655555555556
s2 = str(each)
all_pay = '£' + s2[0:s2.find('.')] + '.' + s2[s2.find('.') + 1:s2.find('.') + 3]
Output:
'£1915.06'
(Sep-29-2024, 12:31 PM)Stephanos Wrote: [ -> ]"The purpose is to extract the first 2 decimal places and not to round up."

Indeed, the str formatting uses rounding.

You could try Decimal.

from decimal import Decimal, localcontext, ROUND_FLOOR


f = 3.325

with localcontext(rounding=ROUND_FLOOR):
    decimal = Decimal(f)
    print(decimal.quantize(Decimal("0.01")))
    print(round(decimal, 2))