Strings

A String is one of the datatypes introduced in the chapter Datatypes. In this chapter, we will expand on that, introducing further operations, and built-in methods for Strings

A string is an iterable object, meaning that you can use Loops on it (other iterable objects include tuple, list, dictionary etc). Strings allow you to iterate over each one of its characters.


Creating a string

To create a string in Python, we use either 'single' or "double" quotes.



So why do we want both single and double quotes? It allows us to use quotes within a string as in the following:



Escape Sequences

Escape Sequences are single characters that are treated as a special character by python. To make an escape sequence, use the backslash ("\") in addition to another character.

The most popular escape character is the newline ("\n"). Putting this character anywhere in the string is similar to hitting enter or return on the keyboard. Remember, even though "\n" is typed with two characters, it is recognized by python as one character.


Other useful escape sequences include



String Operations

Addition and Multiplication

You can use addition to concatenate two strings together. You can also use multiplication to get multiple copies of the same string


in Operation

The in operation lets you check if a string is in another string. Using the operation on two strings would return a boolean value True/False.


Indexing and Slicing

Indexing allows you to get a character from the string. Remember that the count for indexing starts from 0 instead of 1!


You can also do negative indexing, which is indexing but you start the count from the back. In this case, the count for indexing starts from -1. Why not 0? This is because we already used the index 0 for positive indexing.


Slicing is like getting a range of indexes from a certain start and end value. Given a start and end value for the slice, the start value is inclusive and the end value is exclusive. This means that when getting the range of indexes, include index start but exclude index end.



Built-in Methods

str()

Converts another datatype into a string


len()

Given a string, gets the length of the string


chr() and ord()

Each character in a string has an associated ASCII value, an integer between 0 and 127 that represents the character. Here is a table showing characters and their respective ASCII value.

Both chr() and ord() are operations for one character. chr() gives you the character from the ASCII value, while ord() gives you the ASCII value from the character


.capitalize(), .lower(), .upper()

These three operations adjusts the capitalization of the given string.


.isalnum(), .isalpha(), .isdigit(), .isspace()

These methods check whether the contents of the strings are characters in the alphabet ("A", "a", "b" etc.), digits ("0", "1", "2", etc.), or whitespace ("\t", "\n", " " etc.)


.split(), .splitlines()

.split() and .splitlines() are both methods to split a string by what is called a delimiter. A delimiter is a character that indicates where to split a string by. .splitlines() is the special case of .split("\n"). If you call .split() without passing in a parameter for the delimiter, split would just automatically split on any whitespace ('\t', '\n', ' '). The return value of both methods is a List of split strings, which you will learn in the next chapter.


f Strings

F strings are a very powerful new feature in Python 3. It allows for quick string formatting.

Previously, to easily insert variables' values into a string you would have to specify what the type is, as follows:


Now, the f-string allows us to do the previous much easier. Copy this code into VS Code and try it out!

Other Methods

There are a lot of methods for strings, and here we have complied some that are used the most. To learn more about the different methods, you can google "python string built-in methods", which would give you the entire list or documentation of python string methods. There is no need to memorize all the methods, usually with practice you will figure out which ones are useful and which ones are not, and you will automatically remember the ones you have used most.