1. Computing

Beginning Python: Data Types

From , former About.com Guide

6 of 10

Accessing Variables

To access the value of a variable within the Python shell, simply type it.

 >>> x = 1500 
 >>> x 
 1500 

Within a program, one can use the 'print' command.

 cat = 'dog' 
 print cat 
output:
dog

If the variable is a string, you can also access its parts. Each character of the string is indexed, starting with the first character at '0'. By accessing the string with an indexing operator, you can access only part of the string and leave the rest untouched. An example:

 cat = 'dog' 
 print cat[0] 
 print cat[1] 
 print cat[2] 
output:
d
o
g

  1. About.com
  2. Computing
  3. Python

©2013 About.com. All rights reserved.