PROGRAMING/Python

Formatting Strings

donghunl 2024. 8. 23. 03:48
반응형
name = "Darick"
number = len(name) * 3
print("Hello {}, your lucky number is {}".format(name, number))

 

Hello Darick, your lucky number is 18

 

name = "Manny"
print("Your lucky number is {number}, {name}.".format(name=name, number=len(name)*3))
Your lucky number is 15, Manny.
price = 7.5
with_tax = price * 1.09
print(price, with_tax)
print("Base price: ${:.2f}. With Tax: ${:.2f}".format(price, with_tax))
7.5 8.175
Base price: $7.50. With Tax: $8.18
def to_celsius(x):
  return (x-32)*5/9

for x in range(0,101,10):
  print("{:>3} F | {:>6.2f} C".format(x, to_celsius(x)))
  0 F | -17.78 C
 10 F | -12.22 C
 20 F |  -6.67 C
 30 F |  -1.11 C
 40 F |   4.44 C
 50 F |  10.00 C
 60 F |  15.56 C
 70 F |  21.11 C
 80 F |  26.67 C
 90 F |  32.22 C
100 F |  37.78 C

 

fruit = "peaches"
weight = 3.0
per_pound = 2.99

# You are buying 3.0 pounds of peaches at 2.99 per pound.
output = "You are buying {} pounds of {} at {} per pound.".format(weight, fruit, per_pound)
print(output)

# Peaches are 2.99 per pound, and you have 3.0 pounds of peaches.
output = "{1} are {2} per pound, and you have {0} pounds of {1}.".format(weight, fruit, per_pound)
print(output)

# Peaches are 2.99 per pound, and you have 3.0 pounds of peaches.
output = "{fruit} are {price} per pound, and you have {weight} pounds of {fruit}.".format(weight=weight, fruit=fruit, price=per_pound)
print(output)

 

# Here are the items in the customer's basket. Each item is a tuple
# of (item name, weight, price per pound).
#
basket = [
    ("Peaches", 3.0, 2.99),
    ("Pears", 5.0, 1.66),
    ("Plums", 2.5, 3.99)
]


# Calculate the total price for each item (weight times price per pound)
# and add them up to get a subtotal.
#
subtotal = 0.00
for item in basket:
  fruit, weight, unit_price = item
  subtotal += (weight * unit_price)


# Now calculate the sales tax and total bill.
#
tax_rate = 0.06625  # 6.625% sales tax in New Jersey
tax_amt = subtotal * tax_rate
total = subtotal + tax_amt


# Print the receipt for the customer.
#
print("Subtotal:", subtotal)
print("Sales Tax:", tax_amt)
print("Total:", total)

# Print the receipt for the customer. The format string ":10,.2f" 
# works as follows:
#   - ":10" makes the output 10 characters wide.
#   - "," inserts thousands separators between groups of digits.
#   - ".2" limits the output to two digits after the decimal.
#   - "f" tells Python to expect a floating-point number.
#
print("Subtotal:     ${:10,.2f}".format(subtotal))
print("Sales Tax:    ${:10,.2f}".format(tax_amt))
print("Total:        ${:10,.2f}".format(total))

 

Formatting expressions


Expr Meaning Example
{:d} integer value "{0:.0f}".format(10.5) → '10'
{:.2f} floating point with that many decimals '{:.2f}'.format(0.5) → '0.50'
{:.2s} string with that many characters '{:.2s}'.format('Python') → 'Py'
{:<6s} string aligned to the left that many spaces '{:<6s}'.format('Py') → 'Py    '
{:>6s} string aligned to the right that many spaces '{:>6s}'.format('Py') → '    Py'
{:^6s} string centered in that many spaces '{:^6s}'.format('Py') → '  Py  '

 

반응형

'PROGRAMING > Python' 카테고리의 다른 글

Python resources  (5) 2024.06.17
Creating simple POS app using python  (2) 2024.06.12