Intro to Programming Session 2 -f strings Flashcards

1
Q

What is string formatting?

A

Technique used to construct and manipulate strings by inserting variables, values or expressions into predefined placeholders or specificying fromatting rules for data representation within a string

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

String formatting have tools to

A

control text output and making it look ‘nice’

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

What data type are we looking at this session?

A

Dictionaries

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

Usually we have seen one way to print combinations of text and data by

A

joining them together with commas

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

Example 1 of :

Until now we have seen one way to print combinations of text and data: Joining them together with commas:

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

What will this print out?

A

There were 1022 participant

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

Why does this method of printing combinations of text and data by joining them together with commas seem imprecise?

A

For example, if you have floating point numbers it’s nice to be able to limit the number of decimal places - or centre things on the screen.

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

Why does this method of printing combinations of text and data by joining them together with commas seem imprecise?

For example, if you have floating point numbers it’s nice to be able to limit the number of decimal places - or centre things on the screen…

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

F strings are also known as

A

formatted string literals

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

F strings are - (3)

A

concise and readable way to embed expression inside strings in Python, making it easier to interpolate (insert) variables and expressions directly into the string

They allow for easy string formatting

F strings are prefixed with the letter ‘f’ or ‘F’ (just before quote marks) and contain expressions enclosed in curly braces {}

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

Write a piece of code using f string that stores my_variable with 100, has variable foo with f string saying my variable contains value of 100 and print it

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

What is piece of output of this code?

A

My variable contains the value 100

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

What does the ‘f’ tell us in this code? - (2)

A

The ‘f’ tells Python that anything inside curly brackets in the string is a variable -
# it should be

replaced by its value.

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

What is literals? - (3)

A

Refers to raw data that is written directly into source code such as numbers, strings and Boolean values

Values are not variables and used to represent fixed data types

E.g., 42 is numeric literal, “Hello” is a string literal and True is Boolean literal

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

Write a code using fstrings storing 10 to variable ‘a’, storing 15.0 to variable ‘b’ and c containing string ‘test’

Have a f string stored in variable ‘mystr’ which says ‘My vars are and prints values of c,b,a

Then output f string

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

What is output?

A

My vars are Test, 15.0, 10

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

The ways in which variables output in f strings depends on

A

variable type

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

The ways in which variables output in f string depends on variable type

For example… in this code’s output:

A

the float in this case is shown with a .0 on the end whilst the int is not.

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

f-strings allow us to

A

format the data nicely as we print it.

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

When using f-strings, you can include formatting options

such as..

A

placing a colon (:) at end of variable name with some numbers or characters with desired formatting specifications

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

f strings offer ways to format output which includes - (2)

A
  1. Justifying text
  2. Formatting Data
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

f strings offer ways to format output which includes justiying the data meaning - (3)

A

You can specify the width of the field for each variable to ensure consistent spacing in printed output.

By default, f-strings left-align text.

If you want to justify text, you can use < for left justification, > for right justification, and ^ for center justification.

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

f strings offer ways to format output which includes justiying the data for example..

A

by making sure it takes up a certain amount of space for each variable). This is useful when we want to print tables of data.

24
Q

f strings offer ways to format output which includes formatting the data -

A

You can format numerical data using various specifiers.

25
Q

f strings offer ways to format output which includes formatting the data - example

A

for example limiting the number of decimal place in a float or adding zeros to the start of an integer

26
Q

Example of justifying data with f-strings:

If we add ‘:20’ in f strings then it will

A

force our value to take up a certain amount of space by adding, for instance :20 if we want it to take up 20 spaces.

27
Q

Write a piece of code in which it stores string of ‘Test’ into myvar variable

then print just variable using f string with 3 ‘=’ signs around it

A
28
Q

What is output of the code?

A
29
Q

Write a piece of code in which it stores string of ‘Test’ into myvar variable

then print just variable using f string so output looks like this:

A
30
Q

when you provide a string without specifying any alignment in a formatted string, it will be

A

left-alligned by default

31
Q

For example, when you provide a string without specifying any alignment in a formatted string, it will be left-aligned by default.

A

In this example, the string “John” is left-aligned within a space of width 10 characters. Since “John” is 4 characters long, Python adds 6 spaces after “John” to fill the remaining width of 10 characters.

32
Q

Write a piece of code in which it stores string of ‘Test’ into myvar variable

then print just variable using f string with 3 ‘=’ signs around it –> left alligned, right alligned and centre alligned

A
33
Q

By adding a character before the number, we can change the

A

alignment of f-string

34
Q

What’s the output?

A
35
Q

In f strings, if the the specified width is less than the length of the variable, the space expands to

A

accomodate the variable

36
Q

Example:

In Python f-strings, if the specified width is less than the length of the variable, the space expands to accommodate the entire variable.

A

Note that even though we asked for a 5 character space, it expanded to the 12 characters which are necessary.

37
Q

Example:

In Python f-strings, if the specified width is less than the length of the variable, the space expands to accommodate the entire variable.

Even works with numbers

A
38
Q

Exercise:

Print your own name and store it in my var and print it centered in space surrounded by stars like this:

** Alex **

A
39
Q

We can do lots of things to numbers with f-statements such as - (3)

A
  • useful option for integers is to zero-pad the number.
  • Controlling the number of decimal points in floating point numbers
  • Utilise scientific notation to deal with very small or large numbers
40
Q

What does zero-pad the number?

A

means adding 0s before the number to fill the space rather than spaces

41
Q

Example of zero-padding an integer

A

As an example, we could convert the integer 5 into 005

42
Q

When is zero-pad a number

As an example, we could convert the integer 5 into 005

A

This is often useful when naming files to make sure that they sort correctly (e.g. if using participant numbers)

43
Q

Write a code that stores number ‘5’ in mynum variable

Then prints it

Then add 3 0s before it

A
44
Q

Example of using leading zeros (0s that appear before other digits in a numerical value) is useful..

A
45
Q

How to sort a list and print it?

A

list_name.sort()
print(list_name)

46
Q

To control the number of decimal places in floating point numbers it is possible to

A

tune the size of the output by changing both the total number of digits in use and how many come after the decimal point:

47
Q

What would this code produce and why? - (3)

A

== 10.2190==

In the formatted string {myval:10.4f}, the 10 specifies the total width of the formatted string, and .4f specifies that the number should be formatted with four digits after the decimal point.

Since the number 10.219 has only five characters including the decimal point, it’s padded with spaces to meet the specified width of 10 characters, resulting in three leading spaces before the number.

48
Q

What would be output of this code (ignore second line of code) - (4)

A

==10.2190==

In the formatted string {myval:7.4f}, the 7 specifies the total width of the formatted string, and .4f specifies that the number should be formatted with four digits after the decimal point.

Since the number 10.219 has a total length of 7 characters, including the decimal point and digits, it doesn’t require any additional padding.

Therefore, no leading spaces are added before the number.

49
Q

What is output of this code and why - bottom - (4)

A

== 10.22==

In the formatted string {myval:7.2f}, the 7 specifies the total width of the formatted string, and .2f specifies that the number should be formatted with two digits after the decimal point.

Since the number 10.22 has a total length of 6 characters, including the decimal point and digits, it requires 1 leading space to meet the specified width of 7 characters.

Therefore, one leading space is added before the number.

50
Q

Python will round the number if you specify fewer decimal places than the number actually has

  • (2)
A

In Python’s formatting, when you use the f format specifier, you can specify the number of decimal places you want using the notation .nf, where n is the number of decimal places.

For example, if you have the number 10.219 and you format it with :.2f, Python will round it to two decimal places – Output will be 10.22

51
Q

In f string we can use scientific notiation

This is especially useful when dealing with very large or small numbers (e.g. femto-Tesla in MEG). We use the - (2)

A

e formatting character for this.

We can control the total number of spaces and decimal places in the same way as when we used the f formatting character.

52
Q

Example of using scientific notation in f string

A

If you have not seen this notation before, you read it as: “1.0 times 10 to the power -6”, i.e. 1.0×10−6 or 1 million.

53
Q

What is the output of this code?

A
54
Q

What does this code mean? - (3)

The participant ID column will need to be the P plus 3 numbers for the ID. We want the ID zero-padded.

The number of trials column should allow us to have up to 9999 trials, so we should make it 4 characters wide as well.

The reaction time column should have enough space for something like 1999.222 and that three decimal places is sufficient (that is, after all, millisecond resolution which is more than sufficient). This makes us decide that the column should be 8 characters wide.

A

{part_id:03d}: This formats the part_id variable as an integer (d) with leading zeros (0) and a width of 3 characters as want participant’s ID to be zero-padded. The P before the formatted part_id ensures that it’s prefixed with ‘P’.

{num_trials:4d}: This formats the num_trials variable as an integer (d) with a width of 4 characters. Since you want to allow up to 9999 trials, 4 characters are sufficient.

{rt:8.3f}: This formats the rt variable as a floating-point number (f) with 3 decimal places and a total width of 8 characters. This ensures there’s enough space for the reaction time with millisecond resolution.

55
Q

Fix the code below to output a formatted table line for each participant.

The participant ID column will need to be the P plus 3 numbers for the ID. We want the ID zero-padded.
The number of trials column should allow us to have up to 9999 trials, so we should make it 4 characters wide as well.
The reaction time column should have enough space for something like 1999.222 and that three decimal places is sufficient (that is, after all, millisecond resolution which is more than sufficient). This makes us decide that the column should be 8 characters wide.

:
| P{part_id:03d} | {num_trials:3d} | {rt:8.3f} |

P{part_id:03d} | {num_trials:3d} | {rt:8.3f} |

A

Used the loop index i to access elements from part_ids, trial_counts, and rts.
Used correct index i to access the elements from each list.
Applied correct formatting for participant ID (:03d), trial counts (:4d), and reaction time (:8.3f).

56
Q

We often want to ask the user for information. One to do this is the

A

input () function in Python

57
Q
A