Session 2 - Part 1 Flashcards
(145 cards)
What is the term used to describe controlling text output and making it look nice in Python?
String formatting is the process of controlling text output and making it visually appealing in Python.
What is one way to print combinations of text and data in Python before the introduction of f-strings? - (3)
Joining them together with commas,
for example:
numberOfSubjects = 1022
print(“There were “, numberOfSubjects, “ participants”).
What are some drawbacks of using commas to combine text and data for output in Python? - (2)
Commas can be annoying and imprecise especially when trying to format output neatly
E.g., Unable to limit number of decimal places or centre things on screen
There were 1022 participants
What would be output of this code?
2.0 / 3.0 is roughly: 0.6666666666666666
Example image of:
Drawback of using commas to combine text and data for output in Python - unable to limit number of deciaml places example
What is a modern and preferred method for combining data and strings for output formatting in Python, beside using commas?
Using f-strings, which provide a more elegant and concise way to format output by embedding variables directly into strings.
What does f-strings stand for in Python?
An f-string, short for “formatted string literal,”
How do you create an f-string in Python?
Prefix the string with the letter ‘f’, (just before te quote marks) then use curly braces {} to enclose the variable names you want to include in the string.
What happens when an f-string is evaluated in Python?
When an f-string is evaluated, Python replaces the expressions enclosed within curly braces {} with their corresponding values at runtime, resulting in a formatted string.
Explain this code - (4)
the variable my_variable is assigned a value of 100.
Then, an f-string named foo is created, where the value of my_variable (100) is embedded within the string using curly braces {} .
When foo is printed, it will display the string “My variable contains the value 100”.
The ‘f’ prefix before the string indicates that it is an f-string, and Python replaces the expression {my_variable} within the string with the value of the variable during runtime.
What is the output of this code?
My variable contains the value 100
How can you include multiple variables in an f-string in Python?
Multiple variables can be included in an f-string by separating them with commas inside the curly braces {}.
Example of adding multiple variables in f-string in Python
What will be the output of this code?
My vars are Test, 15.0, 10
The way in which the variables are output in f-string depends on the
data type
Example
The way in which the variables are output in f-string depends on the data type
the float in this case is shown with a .0 on the end whilst the int is not.
Explain this code - (4):
three variables a, b, and c are assigned values.
a is assigned 10 (integer)
b is assigned 15.0 (float)
c is assigned ‘True’ (boolean value)
An f-string named mystr is then created, which includes these variables within curly braces {} inside the string.
When mystr is printed, it will display the formatted string “My vars are Test, 15.0, 10”, with the variables c, b, and a inserted into the string in the order specified
What does the term “formatting with f-strings” refer to in Python? - (2)
“Formatting with f-strings” in Python refers to using f-strings to manipulate the appearance of output, such as justifying text or formatting data, by adding specific formatting options after the variable name.
Formatting options always have a colon at the end of the variable name and then some numbers or characters.
What are two ways in which we want to format strings in f-string? - (2)
- Justifying text (for example, 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
- Format the data itself - for example limiting the number of decimal place in a float or adding zeros to the start of an integer
How can you force a value to occupy a certain amount of space using f-strings in Python? -
By adding a colon followed by the desired width after the variable name inside curly braces {} in the f-string.
What would be output of this code?
Q: What does the following f-string do?
myvar = ‘Test’
print(f’==={myvar}===’)
This f-string prints the value of the variable myvar surrounded by equals signs, resulting in the output ===Test===.
Q: Explain the purpose of the following f-string
myvar = ‘Test’
print(f’==={myvar:20}===’): - (3)
The output ===Test === consists of the string “===” at the beginning and end, with the variable myvar (“Test”) occupying 20 spaces.
Output is:
===Test ===
Since the word “Test” has only 4 characters, it is followed by 16 spaces to fill the specified width.