Strings Behind the Scenes - A List of Characters:

  • A string is a list of characters, and each character in this list has a position or an index.

Diagram of a string showing each character with its corresponding index number below it

  • We can get each character by calling its index with square brackets, [ ] as shown in the example below:
python
1greeting = 'Hello World!'
2print(greeting[0])

Output:

1H

String Built-in Function - Len():

  • There are a few built-in functions β€” like len(), which returns the length of a string and is very useful.
  • For example, if we have the greeting variable storing β€˜HELLO WORLD!’, len(greeting) will return 12 as shown below:
python
1greeting = 'Hello World!'
2print(len(greeting))

Output:

112