[SOLVED] String Indeces Must be Integer In Python

String Indeces Must Be Integer In Python

In the past elapsed years, Python has grow so rapidly. From the simple command Python2 into the more sophisticated Python3 with a returning back to more complicated writing rules.

Python2 vs Python3

In Python2, to sum numbers we simply type in the Python interpreter like the following:

>>> print 1+2

However, in Python3, when we execute like that, here is the return:

>>> print 1+2
  File "<stdin>", line 1
    print 1+2
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?

So, in Python3, we must type like this:

>>> print(1+2)

What is an Integer and What is a String

Integer are numbers that can be affected by mathematical operations. For example, 1 and 2. They are summable. However, when the number is intended not to be affected by mathematical operation, then, it must be inserted anywhere as a string. In Microsoft Excel for example, when we are inserting phone number for example 081394033363 it will be treated as numbers for mathematical operation. The zero in the beginning will be omitted. This is not good. To fix this, format the column containing the numbers as column for text.

In Python programming language, to format numbers to be strings, simply add or (single or double quotes) for example:

>>> print '1+2'

The return value is not 3 because the numbers are treated as string.

So, wherever we got error message like indices must be integer, check whether there is data that is intended to be string but written in integer format or vice versa.

Understanding ‘for’ operation in Python Language

When we are working with ‘for’ in a programming language, we must understand that everything in ‘for’ operation must be countable. String data type can also be counted for how many characters in a paragraph with available function in Python language. However, if the ‘for’ operation is intended to loop until a process finish, then the looping index must be in integer format. Otherwise, the operation can not be done.

How to Define Integer and String Data Type

There are so many variables in defining data format in assorted programming languages. For example HTML (actually is not) programming language, treats everything as strings. To change it into integer, in PHP processor, it must be defined by int() function.

Python is different. Since the beginning it is fun to code. When the format is not matching with the syntax, there will be a returning error of how to write it correctly. Moreover, interpreter for both Python2 and Python3 equipped with complete help documentation. The documentation of Python also available everywhere mainly in Python website.

I used to be a pilot, but ended up being just a mediocre writer.