Outputting a string
The print command is used to output a character or string to the user. An example print command can be seen below.
print("Hello world!")
The syntax rules for the basic print statement are:
- Print is written in lower case
- Brackets surround everything to be printed
- Speech marks surround the string to be
printed
Combining strings and values stored in variables
There are three key methods you can use to join strings and values stored in variables: string printing, concatenation and f-strings. I generally tend to use f-strings because of their advantages but it is important to know
the other methods so you can understand other people's code.
String printing
This method only works in print statements. The other two can be used with input statements as well. With string printing, a comma separates each variable and string. When a comma is used the two items are joined and a space
is inserted automatically between them. An example of string printing can be seen below.
The syntax rules for string printing are:
- Print is written in lower case
- Brackets surround everything to be printed
- Speech marks surround each string to be printed
- There is a comma between each variable and string to join them
Benefits of string printing
It doesn't matter what data type is stored as in a variable, the string version of this will be printed.
Drawbacks of string printing
A space is always inserted between strings and values in variables whether you want one or not
Cannot be used in the prompt in input statements
Concatenation
Concatenation is a method that can be used to join strings together. Because it only joins strings, other types of variables must be cast to use them like this. Any spaces to be included must be written inside strings.
The syntax rules for concatenation are:
- Print is written in lower case
- Brackets surround everything to be printed
- Speech marks surround each string to be printed
- There is a plus between each variable and string to join them
- Variables that are not strings are cast as strings
Benefits of concatenation
No spaces are inserted automatically
Can be used in the prompt to an input command
Drawbacks of concatenation
You have to remember to cast non-string variables
Spaces must be added inside strings and can be easily forgotten or misplaced
F-strings
F-strings have been adopted from other programming languages. When we use f-strings there will always be one set of speech marks in a print statement that surround everything to be printed. These speech marks are preceded by a
lower-case f. Values being retrieved from variables are written in braces(i.e. curly brackets { }). This method always prints the string version of variables so no casting is required.
The syntax rules for f-strings are:
- Print is written in lower case
- Brackets surround everything to be printed
- • There is one set of speech marks surrounding the whole
f-string
- • References to variables are inside braces
Benefits of string printing
It doesn’t matter what data type is stored in a variable, the string version of this will be printed
Spaces will be exactly
as typed
Can be used in the prompt to an input statement
There are a range of easy tools to use with f-strings to deal with layout
Syntax checker
String printing
Edit the lines below to produce the correct output using string printing.
name = "Bob" |
age = 32 |
height = 1.67 |
print(name, "is", age) #Speech marks now around string not variables |
print("My name is", name) #print now in lowercase |
print(name, "is", age) #speech marks removed from whole output sentence and placed just around strings |
print(name, "is", height, "meters tall") #second comma added to join string to height |
print(name, "is", age, "years old.") #additional spaces removed |
Concatenation
Edit the lines below so they use string printing to output the desired sentence.
name = "Bob" |
age = 32 |
height = 1.67 |
print(name + " is " + str(age)) #age cast as string |
print("My name is " + name) #print now in lowercase and space added inside string |
print(name + " is " + str(age)) #final missing bracket added |
print(name + " is " + str(height) + "m tall") #second plus added to join string to height and height cast as string |
print(name + " is " + str(age)) #variables no longer inside the speech marks |
F-strings
Edit the lines below so they use f-strings to output the desired sentence.
name = "Bob" |
age = 32 |
height = 1.67 |
print(f"{name} is {age}") #f added at start of string |
print(f"My name is {name}") #print now in lowercase and name is inside the string |
print(f"{name} is {age}")) #round brackets swapped for braces |
print(f"{name} is {height}m tall") #missing speech marks added |
print(f"{name} is {age} years old") #one set of speech marks surround everything to be printed |
<< Previous: VariablesNext: Input >>