Strings in Code
As mentioned above, strings in most programming languages can be declared by single or double quotation marks, and so is the case with Python. Following code block shows some examples of string declaration in Python.
1var book = "The Great Gatsby";
2var author = "F. Scott Fitzgerald";
In Python, characters in a string can be accessed by providing an index
, which is the position of the character that need to be accessed inside a pair of square brackets ([])
. Suppose we wanted to access the character r
in the name
string above. The position of r
in the string is 2. This is because the indices
, or the positions in a string start from 0, and not 1, as illustrated below.

The subsequent code will be:
1const name = "Chris";
2console.log(name[2]); // logs r
Python also has some specified string methods which allow to manipulate strings more easily. These will be discussed in the string operations section below.