Monday, May 29, 2023
HomeUncategorizedPython F-Strings Number Format Cheat Sheet

Python F-Strings Number Format Cheat Sheet

F-String Template

f" Text{replacement_field} Text..."

 Inside quotes, an f-string consists of two parts: (1) A regular string literal, i.e. text and (2) The replacement field

contains the Python expression for evaluation format and format control.

Double quotes are used here, but single quotes or Triple quotation marks.

F-string may contain only one replacement field: f”{replacement_field}”

Replace Field

{f-expression=!conversion:format_specifier}

 A replacement field is represented by a pair of curly braces: { }

 The replacement field consists of a Python expression for evaluation, with optional Debug mode (=), type conversion (!), and format specification (:).

 Substitute into f-string template:

f" text{f expression=!conversion:format_specifier} text..."

Format Specifier

:fill align sign # 0 width sep .precision type

Format Specification Small Language Summary

filled with padding characters
align : Alignment of text within spaces

sign : + and - how to use

# : Alternative representation format for some numeric types

0 : Symbol awareness, zero padding for numbers 
width : Minimum total field width

September : Number separator (',' or '_') .precision : How many digits are displayed for floating point numbers; String maximum field width type : According to data Representation type used by type

Notes : notation , #, 0, September,Accuracy and type versus digital format is of particular interest, focus on this cheat sheet. For general handling of F-Strings, see my cheat sheet Python F-Strings Basics

.

Format Specifier: Option

full

align

symbol

# 0

width

September

.prec

type

char

+

number

_

Number(s)

String

: s

>

-

,

number

: n

^

' '

Integer : d, b,

o, x, X, C

=

symbol

float: e, E, f, F , g, G, %

Signature and Delimiter Options

+

Signs are used for both positive and negative numbers

-

notation is only used for negative numbers ( default)

space

leading spaces for positive numbers and minus signs for negative numbers

separated Device

,

comma. Decimal Integers and Floating Points: Thousand Separator

_

underscore. Decimal Integers and Floating Points: Thousands Separator; b, o, x, and X Types: Every Four Digits Separator

Example: Symbols and Delimiters

#(+): add a sign to both positive and negative number

# sign (-): sign is used only for negative numbers

#Unsigned equals sign (-)

f"{3:+} or {-3:+}"

'+3 or -3 '

f"{3:-} or {-3: -}"

'3 or -3'

#Symbol (space): The leading space of a positive number and Negative sign for negative numbers

# Symbol (+ ), delimiter(,)

f"{3} or {-3}"

'3 or -3'

f"{3: } or {-3: }"

'3 or -3'

f"{10000 :+,}"

'+10, 000'

# padding(?), alignment(

f"{10000:?

'+10,000????'

Sign Aware Padding

Use =Alignment Option

to force padding determined by the symbol to be placed ( Padding and width specifications after the number if any) but before the number. This is useful for number representations like "+000042". This alignment option is only available for number types.

0-option

produces sign-aware zero-padding, Like the=alignment option padded with '0'. It is produced by prepending a '0' to the field width specification.

#fill(?), alignment(=), sign(+), width(5)

# full (0), alignment (=), sign (+), width (5)

f"{3:?=+5}"

' +???3'

f"{3:0=+5}"

'+0003'

# sign(+), 0-option, width(5)

# 0-options,width( 5)

f"{3:+05}"

'+0003'

f"{3:05}"

'00003 '

Decimal integer type

# print symbols 33-41 with unicode decimal representation

Integer representation type

d

Decimal Integer: Number in base 10

other

not any

as d

n

number. Same as d, except that the current locale is used to set the appropriate number separator

f "binary: {i:#b }; octal: {i:#o}; hex: {i:#x} or {i:#X}"

b

# #-option Add prefix
Binary: A number in base 2.

o

Octal: A number in base 8.

i=13

# Represents decimal 13 in binary, octal and hexadecimal

f"binary: {i:b}; octal: {i:o}; hex: {i:x} or {i:X}"

x, X

Hexadecimal: a base 16 number. x and X for lowercase and uppercase for numbers> 9

C

Unicode characters. Convert decimal integer to corresponding unicode character

#-option: use# - option to add prefix

#b: Add prefix 0b #o : Add prefix

0o

#x: add prefix 0x #X: add prefix 0X

Example: Integer Type

'Binary: 1101; Octal: 15; Hexadecimal: d or D'

f"{i:0=#8b}"

'0b001101'

# returns the decimal representation unicode character 36

# fill (0), align (=), #-option, width (8), type (b)

' Binary: 0b1101; Octal: 0o15; Hex: 0xd or 0XD'

# Binary zero padding

# Binary file with prefix and zero padding

f"{i:08b}"

'00001101'

f"{i:#08b}"

'0b001101'

f"{36:c}"

'$'

print(''.join([f"{x:c}" for x in range(33,42)]))

!"#$%&'()

# type d is the default value for integers, so no need

# delimiter (,) and class Type(d)

f"{10000:,d}"

'10,000'

f"{10000:,}"

#Use the locale specific number format, set the locale

Import Locale

locale.setlocale(locale.LC_ALL, '')

# Then use format type 'n'

'10,000'

#Separator(_),Type(b)

F"{ 1200:_b}"

'100_1011_0000 '

f"{106:n}"

'1,000,000'

Floating point representation type

type

Name

For a given precisionp...

e, E

Scientific notation

coefficients have 1 decimal point and p digits after the decimal point.

# lowercase g and uppercase G

f, F

Fixed-point notation

Yesp digits after the decimal point.

G, G

General format

round ds numbers are converted to p significant digits, and then according to the size in fixed point or Formatted in scientific notation.

%

percentage

Multiply the number by 100 and display it in fixed f format followed by a percent sign.

 The default value for floating point numbers is p=6. Default value in decimal. The decimal p is large enough to display all coefficient digits.

 Lowercase and uppercase: Make sure scientific notation is to display "e ' or 'E' ' and whether to use 'nan' versus 'NAN' and 'inf' versus 'INF'.

Example: Simple floating point comparison

# %-style with precision=1

# lowercase e and uppercase E

#lowercase f and uppercase F

f"{1/2:.1 %}"

'50.0 %'

f"{1000:.1e} with {1000:.1E}"
'1.0e+03 and 1.0E+03'

f"{1000:.1f} and {1000:.1F}"

'1000.0 vs 1000.0'

# Lowercase and uppercase NaN means

F "{1000:.1g} and {1000:.1G}"

'1e+03 and 1E+03'

nan=float('nan')

f"{nan: f} vs {nan:F}"

'nan vs NAN'

Previous article: Healthcare inflation is latest price hike to hit consumers coping with rising costs
Next articleMidlife crisis of nearby stars illuminates future of our sun
inew
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

LAST NEWS

Featured NEWS