Week 3: Loops in Python Flashcards
what are print() arguments?
print() arguments change the default ways that print() command behaves.
what is the end= argument
a print() argument that changes the default ending of the print() command.
- , end=”” removes the default line break and replaces it with nothing.
- we can replace the line break with whatever we put in the quotation marks
what is the sep= argument
a print() argument that changes the default separator (a single space)
- sep=”” just removes the separator and replaces it with nothing
- we can change the separator to be whatever we put in the quotation marks
What is string interpolation?
when you read in values of variables or expressions into placeholders in the string
What are the 3 methods of doing string interpolation/string formatting
- f-string
- % Operator
- The .format() method
What are f-strings, their syntax, what they are used for, how they work
f-strings are strings that start with an f before the quotation marks and values can be interpolated directly into the string by surrounding them with brackets {}
Expressions can also be embedded in f-strings and the result of the expression will be interpolated into the string
what are format specifiers? do f-strings support format specifiers?
format specifiers give us more control of the output when formatting strings and interpolating values.
f-strings support many different format specifiers
what is the :.xf format specifier, when is it used? give an example
the :.xf format specifier is used in f-strings to round the value to x decimal places
What does the % operator do when used on strings, what is the syntax
when used on strings, the modulo operator allows for formatting by replacing placeholders with variable values - allows you to interpolate values into strings
reads in the values from left to right in the string in the order you type them
What are some common placeholders using the % operator
- %s : for strings
- %d : for integers
- %f : for floating-point
What is the .format() method? What is it used for, what is the syntax? What are the 3 different ways to do it?
The .format() method is an easy way to format and interpolate values into strings
This method supports 3 different ways to replace brackets {} with values.
- Empty replacement fields
- Replacement fields with zero-based indices
- Replacement fields with Named Arguments
Explain how the .format() method works with empty replacement fields. Give an example of the syntax
{} act as replacement fields in the string we are interpolating values into.
It reads the values into the string from left to right in the order you type them in the parentheses
Explain how the .format() method works with replacement fields with zero-based indices, give an example
the first value typed in the parentheses will go to index {0}, the next will go to index{1} and so on
Explain how the .format() method works with replacement fields with named arguments, give an example
it does not matter what order you type the value of the variables in the parentheses because they will go to the replacement field of their name
Compare the 3 string interpolation methods based on syntax, readability, variable insertion, type safety, performance, expressions, templates/reuse, and legacy
- f-strings are the most readable, it directly embeds expressions, can support full expressions directly, no explicit type required (can use without specifying type), fastest method, no built-in template support, newer (Python 3.6+)
- % operator is less readable for complex cases, requires placeholders for variable insertion, requires correct types for placeholders, generally slower than f-strings, no expressions - only variables, limited and inflexible, old method but still used
- .format() method is more readable than % operator, uses {} as placeholders, more forgiving with types, slower than f-strings, can use methods/formatting in {}, can create reusable templates, introduced in Python 2.7