Strings
What is a string?
Strings are how you represent text in Python. Anytime you need to work with
text, you'll need strings.
A string is simply some letters in a row. These are surrounded by quotation
marks.
Here are three examples of strings:
"test"
"this is a string"
"hello world"
Strings can be put into print
just as with numbers. This displays text in
the output box:
print("test")
Run the code and see what happens to continue.
hello world
Conventionally, the first program people write when they try a new language is
one that displays the text hello world
.
To continue, write code that prints hello world
to the output box.
Where are strings used?
Strings are used in nearly every Python program.
For example, you'll need strings if your program deals with any of these:
- usernames or passwords
- email addresses
- error messages
- links
There are countless examples. Feel free to come up with more.
See why quotes matter
In Python, strings must be surrounded by quotation marks. If you forget,
you'll get an error.
The program in the code box is broken. Try to run the program to see the error
in the output box. Then fix the program.
Combining strings
You can use the +
sign to combine two strings into one string. Programmers
call this concatenating two strings.
Here is an example:
print("pro" + "gram")
Try concatenating strings
Write a program that prints the result of concatenating the strings "fire"
and "works"
.
Printing strings and numbers
Write a program that uses print
twice. Print a string, then calculate and
print a number.
Your program should print the string "856 * 412 ="
on the first line.
Your program should calculate and print 856 * 412
on the next line.
In other words, your program should output something like this, but with
different numbers:
10 * 10 =
100
If you need help, try pressing the 'Hint' button.