Print
- In Figure 4, the first print statement is printing the previously defined variable myName, since the script editor is in immediate mode the information from the previous line is still available. The same is completed with the variable myAge.
- These two print statements are used to show the results of printing a string variable and a numeric variable.
- When using a print statement all variables must be the same type. In the third print statement, variables are being combined together to give a line of text. The first text is directly inputted without being part of a variable, next the plus sign and the variable myName is inputted. Quotes are used again between the two plus signs to create a blank space. After the third plus sign additional text is inputted and then finally the myAge variable will be interpreted as a string to complete the printed statement. The final displayed line is the result of this combination. Without the string (str) command an error would have been generated. The values of the variables are still available from the previous lines executed, this will be true unless the variable is redefined or the editor is closed.
Printing only Part of a Variable
- The myName variable is still defined in the PythonWin editor.
- In figure 5 the first line of code prints character space 0 to character space 5. The first position is 0 in a variable. The sixth position (number 5) is not printed. Therefore, positions 0, 1, 2, 3, 4 are printed.
- The next print state would print positions 6, 7, 8, 9, 10, 11.
- The format uses square brackets with a colon separating the positions.
- The third example is printing only a single character, which is position 6.
Printing a List
Two lists were previously defined and are still available in the PythonWin program. In figure 6, the print statement and the variable name outputs the complete list. The first list is a set of addresses and the second list is a set of whole numbers. Note the square brackets around the values in the lists.
Note in figure 7 instead of just placing the name of the variable after the print statement that the number two was placed after the name surrounded by square brackets. Therefore, the returned result is what was in the third position which is represented by the number 2. When a list variable is queried it brings all the items of the specified position(s) a simple variable returns individual characters.
In figure 8, the items designated in the square bracket will return the result of position 1 and 2, but will not print position 3. The results included in the square brackets use a similar method that was shown with individual variables.