Skip main navigation

Recap: Input, print and f-strings

Learn algorithms, logic, and Python basics. Create simple programs, grasp computer science fundamentals, and see its impact across various fields.

We talked about the input and print functions, as well as f-strings. In the next labs, you will use these concepts. Here are some important details to recap what we covered so far.

The input function

The input function allows us to read input from a user as a string (text). We can use it like so:

my_variable = input('Please provide an input: ')

Note that above we provide a prompt text (above the prompt is “Please provide an input: “) which is printed to the user before they are prompted for their input. It’s therefore important to provide an informative prompt to let the user know that the program is waiting for them to provide an answer. From the text above it’s clear that the user is asked to provide an input, for example. If we were to ask for an amount in GBP, we could use as a prompt: "Please provide the amount in British pound sterling (GBP): "

Numeric values

The input function, even if the user provides a numeric value, will always read a text/string. If you want to read numeric values (integers or floats) you will need to convert them to numeric values. To do this, you can use the int and float functions like so:

# Reading an integer
my_var = int(input('Provide an integer value: '))

# Reading a float
my_var = float(input('Provide an integer value: '))

The print function

The print function can be used to present output to the user. You can print something like so: print('Hello world'). You can also print a variable like so: print(my_var) (note that we don’t have quotes around the variable here). See some example below:

# Raw text
print('This is simple text without variables, just raw text.')

# Priting just variable (note that in the print, my_name has no quotes)
my_name = 'Rafael'
print(my_name)

# Printing just variable again but a numeric one
age = 21
print(age)

# Printing an empty line (just passing nothing to the print function)
print()

f-strings

The f-string allows us to define strings with dynamic values. You need to use the leading f before quotes like so: f'Hello world {my_var}' note that now you can write some string (Hello world) yet specify a variable (my_var) that will be automatically expanded by Python and replace with the contents of the my_var variable. Note the curly brackets surrounding variables. Remember, for f-strings to work in Python, you need the f letter before the quotes! See some examples below:

my_name = 'Rafael'
my_string = f'Hello world, my name is {my_name}'
print(my_string) # prints: Hello world, my name is Rafael

# You can have multiple variables in an f-string
name = 'Rafael'
job = 'Research Fellow'
school = 'School of Computing'
institution = 'University of Leeds'

print(f'I am {name}, and my job title is {job}. I am based in the {school} at the {institution}.')
# The above prints: I am Rafael, and my job title is Research Fellow. I am based in the School of Computing at the University of Leeds.
This article is from the free online

An Introduction to Programming Using Python

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now