Output With Variables
Part of the Fundamentals section of Coddy's Python journey — lesson 25 of 77.
As of now we learned how to print simple strings, but sometimes we need to insert variables values into the string.
For example:
age = 10
print("His age is: age")This will print "His age is: age" instead of "His age is: 10"
To make it work, we will use f-strings:
age = 10
print(f"His age is: {age}")This prints "His age is: 10"
Before the quotation marks "" we add the letter f and inside the string, wherever we put curly braces {} it will insert the value of what is written inside it.
Challenge
BeginnerYou are given a code that stores a random string as input to a variable named rnd.
Print to the console "The input is: " and the random string that is inside the variable rnd.
Check the test cases for examples!
Cheat sheet
To insert variable values into a string, use f-strings:
age = 10
print(f"His age is: {age}")
# Output: His age is: 10F-strings are created by adding 'f' before the quotation marks. Variables are inserted using curly braces {}:
variable = "value"
print(f"Text {variable} more text")Try it yourself
rnd = input() # Don't change this line
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 48Loops
For LoopWhile LoopBreakContinueRecap - FactorialThe Range FunctionNested LoopRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators9Functions
Declare a FunctionArgumentsReturnRecap - Sigma FunctionRecap - Validation FunctionDefault Values12Iterating Over Sequences
Iterating Over ElementsThe Enumerate FunctionIterating Over Strings Part 1Iterating Over Strings Part 2