Hardware Reference
In-Depth Information
continued
Just like in your Hello Minecraft World adventure, print() displays the word
hello . The quotes around the word tell Python that you want to display the word
hello exactly as it is given. If you put spaces between the quotes, those spaces
will be displayed too. The text between the quotes is called a string .
Here is another type of print() :
print(pos.x)
print() displays the value inside the pos.x variable. This is a number, but the
number might change every time you use the print() statement depending
on what value is stored in that variable.
Finally, you need to understand what str() does in your print() statement.
Python print() gives you many different ways to mix numbers and strings
together. When you mix numbers and strings inside Python, as in the following
code, you have to tell Python to convert the numbers to strings so that they can
be tacked on to the rest of the characters in the string that is being displayed.
str() is built in to Python and it converts whatever variable or value is between
its brackets into a string. The plus symbol ( + ) tells Python to join together “x=.”
and whatever is stored in the pos.x variable to make one big string, which is
then printed onto the Python Shell window.
print("x= "+str(pos.x))
Why don't you try this line without the str( ) to see what happens:
print("x= "+ pos.x)
A string is a type of variable that can store a sequence of letters, numbers, and
symbols. It is called a string because you can imagine a long piece of string with
beads that you slide on it like a necklace or a wrist band. Each bead could have a
different number, letter, or symbol printed on it. The string then keeps them all in
the same order for you.
You can use strings for many different purposes, such as storing your name, or an
address, or a message to display on the Minecraft chat.
 
Search WWH ::




Custom Search