Lesson 2 of 4
Variables and Strings
A variable is a name that points to a value. You create one with =:
Python figures out the type for you: "Bilbo" is a string (str), 111 is an integer (int), and 3.5 would be a decimal (float).
f-strings
An f-string lets you drop variables straight into text. Put an f before the quotes and the variable in {}:
Strings have handy methods like .upper(), .lower(), and .title().
Functions that return
A function is a reusable recipe. def defines one, and return hands a value back to whoever called it:
Exercise: Welcome sign
Write a function greet(name) that returns the string Welcome to the Shire, <name>!
For example, greet("Sam") returns "Welcome to the Shire, Sam!".
Your turn!
Write your code in and press Submit answer.
- print(greet("Sam")) → Welcome to the Shire, Sam!
- print(greet("Rosie")) → Welcome to the Shire, Rosie!
- plus 1 hidden check