top of page
top

Python 2a - Inputting Text

noun-input-1454995-FFFFFF.png

Inputting Text (Strings) in Python

string is a collection of characters (letters, numbers and punctuation) such as: “Wednesday”, “Toy Story 4” or “Boeing 747”.

Use the input command to ask a question and let a user input data, which is automatically stored as a string.

Variable to save the answer into. Give it a suitable name based on the input.

name = input("What is your name? ")

=

What is your name? Paulina

Type your answer directly into the editor and press the Enter key.

Statement that is printed to the screen. Leave a space to make the output look clearer.

Once an input has been saved into a variable, it  can be used for other purposes, such as printing it within a sentence:

name = input("What is your name? ")

print("It is nice to meet you" , name)

=

What is your name? Jake the Dog

It is nice to meet you Jake the Dog

Always choose an appropriate variable name when using inputs.

colour = input("What is your favourite colour? ")

print("Your favourite colour is " + colour + "? Mine is yellow.")

=

What is your favourite colour? blue

Your favourite colour is blue? Mine is yellow.

Inputting Text Task 1 (Holiday)

Write an input line to ask the user where they last went on holiday.

 

Write a print line that uses the holiday variable (their answer).

Example solution:

Where did you last go on holiday? Scotland

I hope you had a nice time in Scotland

Inputting Text Task 2 (New Neighbour)

Write an input line to ask the user for a title (e.g. Mr, Mrs, Dr).

Write another input line for an object.

Write a print line that uses both input variables (title and object).

Example solutions:

Enter a title: Dr

Enter an object: Fridge

I think my new neighbour is Dr Fridge

Enter a title: Mrs

Enter an object: Armchair

I think my new neighbour is Mrs Armchair

Using a Variable Within an Input

To use a variable you have previously assigned a value to within the input statement you must use + (commas will not work).

drink = input("What would you like to drink? ")
option =
input("What would you like with your " + drink + "? ")
print("Getting your" , drink , "and" , option , "now....")

=

What would you like to drink? tea

What would you like with your tea? biscuits

Getting your tea and biscuits now...

What would you like to drink? apple juice

What would you like with your apple juice? cake

Getting your apple juice and cake now...

Inputting Text Task 3 (Name & Game)

Ask the user what their name is.

Ask the user what their favourite game is.

 

Use their name in the input statement for their game.

Print a response with their name and the game they entered.

Example solutions:

What is your name? Rory

Hi Rory, what's your favourite game? Minecraft

Rory likes Minecraft? That's nice to know.

What is your name? Kayleigh

Hi Kayleigh, what's your favourite game? Stardew Valley

Kayleigh likes Stardew Valley? That's nice to know.

bottom of page