Tkinter

Python Strings

Python Strings

A string in Python is a collection of characters surrounded by single, double, or triple quotations. The computer does not recognise the characters; instead, it records the altered characters as a series of 0s and 1s.

The ASCII or Unicode character is used to encode each character. As a result, Python strings can alternatively be referred to as a collection of Unicode characters.

Strings are generated in Python by enclosing a character or a sequence of characters in quotations. Python allows us to use single quotes, double quotes, or triple quotes to build the string.

String creation in Python

By surrounding the characters in single-quotes or double-quotes, we can form a string. Triple quotes are another way to express a string in Python, but they're usually reserved for multiline strings or docstrings.

Output

Indexing and splitting strings

The indexing of Python strings begins at 0 as it does in other languages. For instance, the string "HELLO" is indexed, as shown in the diagram below.

Reassigning Strings

It's as simple as assigning a new string to update the content of the strings. The string object does not enable item assignment, which means that a string can only be partially replaced with a new string. In Python, strings are immutable.

Getting rid of the String

Strings are immutable, as we all know. The characters in the string cannot be deleted or removed. The Del keyword, on the other hand, can be used to remove the entire string.