Game Development Reference
In-Depth Information
3 - Strings
strings, we put them in between two single quotes ('), like this:
>>> spam = 'hello'
>>>
The single quotes are there only to tell the computer where the string begins and ends
(and is not part of the string value).
Now, if you type spam into the shell, you should see the contents of the spam variable
(the 'hello' string.) This is because Python will evaluate a variable to the value stored
inside the variable.
>>> spam = 'hello'
>>> spam
'hello'
>>>
Strings can have almost any character or sign in them as well as spaces and numbers.
(Strings can't have single quotes inside of them without using an escape character. Escape
characters are described later.) These are all examples of strings:
'hello'
'Hi there!'
'Albert'
'KITTENS'
'7 apples, 14 oranges, 3 lemons'
'A long time ago in a galaxy far, far away...'
'O*&#wY%*&OCfsdYO*&gfC%YO*&%3yc8r2'
As we did with numerical values in the previous chapter, we can also put string values in
expressions. For example, the expression 4 * 2 + 3 is an expression with numerical
values that will evaluate to the integer 11 .
String Concatenation
You can add one string to the end of another by using the + operator, which is called
string concatenation. Try entering 'Hello' + 'World!' into the shell:
>>> 'Hello' + 'World!'
'HelloWorld!'
>>>
Search WWH ::




Custom Search