Intro to Programming Session 2 -f strings Flashcards
What is string formatting?
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
String formatting have tools to
control text output and making it look ‘nice’
What data type are we looking at this session?
Dictionaries
Usually we have seen one way to print combinations of text and data by
joining them together with commas
Example 1 of :
Until now we have seen one way to print combinations of text and data: Joining them together with commas:
What will this print out?
There were 1022 participant
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.
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…
F strings are also known as
formatted string literals
F strings are - (3)
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 {}
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
What is piece of output of this code?
My variable contains the value 100
What does the ‘f’ tell us in this code? - (2)
The ‘f’ tells Python that anything inside curly brackets in the string is a variable -
# it should be
replaced by its value.
What is literals? - (3)
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
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
What is output?
My vars are Test, 15.0, 10
The ways in which variables output in f strings depends on
variable type
The ways in which variables output in f string depends on variable type
For example… in this code’s output:
the float in this case is shown with a .0 on the end whilst the int is not.
f-strings allow us to
format the data nicely as we print it.
When using f-strings, you can include formatting options
such as..
placing a colon (:) at end of variable name with some numbers or characters with desired formatting specifications
f strings offer ways to format output which includes - (2)
- Justifying text
- Formatting Data
f strings offer ways to format output which includes justiying the data meaning - (3)
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.