f-strings Flashcards

(7 cards)

1
Q

print(f”The quotient of {num1} and {num2} is {num1/num2:.2f}”)

A

any variable or python expression can be inserted directly into {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

floating point formatting

A

print(f’{435:06.2f}’)

0 means fill the extra space with 0 (only allowed for numbers , if not specified defaults to spaces)

6 means the format should take 6 characters

.2 means 2 decimal point

f means number should be displayed as floating point number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

string formatting

A

print(f’{“big long string”:>5.2s}’)

the > will format the string to the right

^ will centre the value in the centre of the window

5 is the output should take characters

.2 is use a maximum of 2 characters from the string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

my_num = 9743
print(f’With commas: {my_num:,d}’)

A

a comma separating the thousands

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

d

A

for integer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

g

A

for ‘optimate’ float notation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly