Python Basics - Strings

A string is a sequence of characters. A string can take the form of a literal constant (fixed value) or of a variable.
A string in Python can be enclosed by double ("...") or single ('...') quotes. Single quotes are used more frequently. Interestingly, double quotes can contain single quotes, for example:
"Don't quote me on that".
The exact opposite is also true, single quote strings can contain double quotes.
Below is an example of a string stored in a variable (s), enclosed by single quotes:

Concatenation

A string literal is the nomenclature used to describe a sequence of characters from the source (first) character, all the way to the end of what is enclosed in quotes.
In Python, strings are "immutable", in computer science the term immutable is used a lot, it simply means that what it describes is unable to be changed. Because strings are immutable, in order to create a "new" string, an expression could construct this like so ('new' + 'string') which would construct this string 'newstring'.
The length of a string can be returned by using the "len" function. The '+' operator concatenates strings. Concatenate is the nomenclature used to describe the action of linking together in a chain or series.

When two or more string literals are placed next to each other, they are automatically concatenated:

Besides concatenating strings, it is also possible to repeat a string with '*' :

String indexing

To access a character within a string, the [] syntax can be used. This is called 'string indexing'. Python uses zero-based indexing, meaning that the first character can be assessed using 0, the second using 1.

Indices can also be negative numbers, to start counting from the right instead of the left:

Data type conversion

In Python, the use of '+' does not convert from other data types, for example a variable holding an integer. In this case the str() function can be used, as seen below. The str() function converts values to a string data type. After this point they can be treated as any other string, for example they can be combined with other strings:

String Methods

Python is an Object Oriented Programming (OOP) language. Therefore, methods in Python run on 'objects'. A method is like a function, but it runs on an object. For example, 's' is a string variable, this is now the object. From here, Python has many built-in methods that can run on this object. Below are two examples, the first uses the method lower(), which converts the string literal entirely into lower case. The second, into entirely upper case:

Other useful string methods are the isalpha() and isnumeric(), which return true for a string that contains only alphabets, or numerics, respectively.

String Slices

A string 'slice' can be thought of as cutting into parts of a string. The slice s[start:end] takes the elements of the string from the very start, up to but not including the end. For example, s[0:3] from below will return a string value of 'cut'.

stringslice

It is also possible to concatenate string slices:

It is also possible to omit either the start or end index, or both. In this case the index will default to the very start or end of the string, for example:

Striding

An additional ':' making for a total of three indices can be used to 'stride' or 'step'. This will step to the given value after each character in the slice is retrieved. For example, the string holds binary numbers that repeat twice before changing value. By setting the value of the third index to 2, the slicing steps over the repeated values and then prints alternating binary numbers without repeating:

F-Strings

F-strings provide a concise and convenient way to embed python expressions inside string literals for formatting. The f string will replace the variables with their values when the string is displayed. The 'f' stands for format, as Python formats the string by replacing the variable name in braces with the value it holds: