Printing a string - Tom Kleen



Formatting a number with an f-stringUse the following format for printing out variables using an "f" string:{<variable>:<fieldwidth>[,][.]<decimalPlaces><datatype>}<variable> . the variable you want to print<fieldwidth> the total number of columns in your number including decimal point, commas, and digits to the right of the decimal point.[,]—an optional comma (for numbers 1,000 or greater)[.]—an optional decimal point (for floating point numbers only)<decimal places>: number of digits to the right of the decimal point<datatype>: "f" for floating point, "d" for integer (decimal)—not requiredPrinting a stringStrings are easy:name = "George"print(f"The name is {name}.")Printing an integercount = 1500print(f"The count is {count:,}.")print(f"The count is {count:10,}.")Printing a floating point numbercost = 1234.56print(f"The cost is {cost:0,.2f}.")print(f"The cost is {cost:10,.2f}.") ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download