iNTERFACEWARE Products Manual > Learning Center > Learning Python > Working With Values and Variables > Basic Values: Numbers and Strings |
|
Looking for Iguana v.5 or v.6? Learn More or see the Help Center.
Two of the basic building blocks of any Python script are numbers and strings. A number in Python is just like what it is in the real world: one or more digits between 0 and 9 that collectively represent a numeric value. This collection of digits may be preceded by a negative sign (-) and may contain a decimal point. There are two basic types of numbers:
Here are some simple examples of numbers:
A string is a collection of zero or more symbols. These symbols (often called characters) can be any combination of letters, numbers, or anything else that can be displayed on a screen or stored in a file. Strings are normally enclosed in either double quotes (" ") or single quotes (' '). Here are some examples:
As you can see, each string in the above examples is surrounded by a pair of double quotes or a pair of single quotes. These quote characters enable Python to determine where a string begins and ends. Quote characters are not part of the string; they just enclose it. The last string in the list above, "", is called the empty string; it is a string of length zero. If your string contains the character that you are using as the quote character, use a backslash (\) to indicate that the character is not a quote character:
This is stored in the computer as:
The backslash is also referred to as the escape character.
To create a string in Unicode format, put a u character in front of the string:
To include a special character in the Unicode string, use \u followed by a four-digit number in hexadecimal format representing the numeric value of the character:
It is important to remember that the number 714 and the string '714' are not the same thing in Python, though you can convert one to the other under certain conditions. String and numeric conversions will be discussed in the next section. |