Introduction to Python Strings
A string in Python is an ordered collection of characters used to store and represent text-based information. From a functional perspective, strings can be used to represent just about anything that can be encoded as text. They can also be used to hold the absolute binary values values of bytes and multibyte Unicode text.
You may have used strings in other languages, and Python’s strings serve the same role as character arrays in languages such as C. In C, we might see a statement such as this:
char ch = ‘a’;
If we want to have a string, we would use something like this:
char *str = “Some arbitrary string”;
or:
char str[] = “Some arbitrary string”;
But in either case, our string is actually an array of characters. Python has no distinct type for individual characters; instead you just use one-character strings. Also, unlike in C, strings in Python are a somewhat higher-level tool and come with a powerful set of processing tools.
Python strings are categorized as immutable sequences, meaning that the characters they contain have a left-to-right positional order and that they cannot be changed in place. In fact, strings are a subset of the larger class of objects called sequences.
There are many ways to write strings in Python. This is a valid Python string:
a = ‘string’
But then again, so is this:
a = “string”
You can also use triple quotes:
a = ”’…string…”’
a = “””…string…”””
Around Python strings, single and double quote characters are interchangeable. That is, string literals can be written enclosed in either two single or two double quotes – the two forms work the same and return the same type of object. The reason for supporting both is that it allows you to embed a quote character of the other variety inside a string without escape it with a backslash. You may embed a single quote character in a string enclosed in double quote characters, and vice versa:
>>> ‘string”s’, “string’s”
(‘string”s’, “string’s”)
Incidentally, Python automatically concatenates adjacent string literals in any expression, although it is almost as simple to add a + operator between them to invoke concatenation explicitly:
>>> a = “Some ” ‘arbitrary’ ” string”
>>> a
‘Some arbitrary string’
Note that adding commas between these strings would result in a tuple, not a string. Also notice in all of these outputs that Python prefers to print strings in single quotes, unless they embed one. You can also embed quotes by escaping them with backslashes:
>>> ‘string\’s’, “string\”s”
(“string’s”, ‘string”s’)
Recent Comments