Strings In Python

Overview:

Strings are used for representing and processing texts of arbitrary length.

In other programming languages like C, C++ and Java there is a separate type representing characters. In those languages strings are collection of characters.

In Python there is no separate type to represent characters.

Python strings are a collection of code points.

In Python, individual characters as well, are represented as strings.

Strings are immutable in Python.

Strings in Python are of type str which is a built-in class in Python.

String Construction:

Strings literals can be constructed in multiple ways using:

Single quotes

Double quotes

Triple quotes

Constructor

Single Quoted Python Strings Vs Double Quoted Python Strings:

In Python, there is no difference between strings constructed using single quotes and double quotes. It is good if the developer could stick to one convention.

If a string contains a single quote or double quote - use the other one to avoid escaping it with a backslash, which improves readability.

Triple Quoted Strings:

Triple quoted strings are used to provide string literals that span across multiple lines in a Python Program. All the white spaces are taken as part of the string literal. Triple quoted strings are also used as docstrings in a Python Program.