1. What is a String?
A string is an immutable sequence of one or more Unicode characters. In Python, strings are created by enclosing a sequence of characters inside a pair of either single quotes ('...') or double quotes ("...").
2. Using Escape Sequences Within Strings
An escape sequence is a sequence of characters used within a string which is translated into another character or sequence of characters that are otherwise not possible to put directly into a string. In Python, the backslash () is called the escape character. Escape sequences start with the escape character () and consist of one or more characters. For example, n is an escape sequence which represents a newline character.
3. Raw Strings
A raw string in Python is a string which treats the escape character, i.e. the backslash (), as a literal character. Python raw strings are helpful in situations when we don't want the backslash to be treated as the escape character and have it as part of the string. A Python raw string is created by prefixing a string literal with 'r' or 'R'.
4. Multiline Strings in Python
Multiline strings in Python can be created using either three single quotes or three double quotes.
5. String Concatenation
Concatenation is the process in which we append one string to the end of another string. In Python, strings can be concatenated using the + operator.
Two string literals next to each other are automatically concatenated even without the + operator as shown below:
It is important to note that the above approach works only with string literals. Trying to concatenate a string literal with a variable or an expression without the + operator will produce an error as shown below:
6. String Repetition
In Python, a string can be repeated a specified number of times using the * operator as shown below:
7. String Methods
In Python, strings support a number of methods to perform common operations. Some of the most commonly used Python string methods are listed below. It is important to note that strings are immutable in Python. Therefore, all string methods return new values without changing the original string.
It converts a string into lower case.
It converts a string into upper case.
It converts the first character of a string to upper case.
It converts the first character of each word of a string to upper case.
It swaps cases of the characters of a string. Lower case becomes upper case and vice versa.
It returns the number of times a specified value occurs in a string.
It returns true if the string starts with the specified value.
It returns true if the string ends with the specified value.
It searches the string for a specified value and returns the position of where it was found. It returns -1 if the value is not found.
It searches the string for a specified value and returns the last position of where it was found. It returns -1 if the value is not found.
It searches the string for a specified value and returns the position of where it was found. It raises an exception if the value is not found.
It searches the string for a specified value and returns the last position of where it was found. It raises an exception if the value is not found.
It returns True if all characters in the string are alphanumeric, meaning alphabet letter (a-z) and numbers (0-9).
It returns True if all characters in the string are alphabet letters (a-z).
It returns True if all characters in the string are decimals (0-9).
It returns True if all characters in the string are digits, otherwise False. It considers exponents, like ², as digits.
It returns True if all characters in the string are in lower case. It checks only alphabet characters. Numbers, symbols, and spaces are ignored.
It returns True if all characters in the string are in upper case. It checks only alphabet characters. Numbers, symbols, and spaces are ignored.
It returns True if all words in a string start with an upper case letter, and the rest of the word are lower case letters. Numbers, symbols, and spaces are ignored.
It removes the specified leading characters from a string. Space is considered to be the default leading character to remove.
It removes the specified trailing characters from a string. Space is considered to be the default trailing character to remove.
It removes the specified leading and trailing characters from a string. Space is considered to be the default trailing character to remove.
It splits a string into a list at the specified separator. Any whitespace is considered to be the default separator. This method accepts an optional second parameter called 'max'. If this is specified, the list will contain the specified number of elements plus one (max + 1).
It splits a string into a list and the splitting is done at line breaks. This method accepts an optional second parameter called 'keeplinebreaks'. It specifies if the line breaks in the string should be included (True), or not (False). The default value is False.
It replaces a specified phrase within a string with another specified phrase.
8. String Indexing
Since strings in Python are sequences made up of characters, they can be indexed, meaning that each of a string's characters corresponds to an index number. The first character has an index 0.
Accessing Characters Using a Positive Index Number
We can use a positive index number in square brackets and access a character as shown below:
Accessing Characters Using a Negative Index Number
Python also supports negative index numbers. We can use negative indices with strings to start counting from the right, starting at the index number -1.
9. String Slicing
String slicing is the process of extracting a range of characters from a string. We can create a slice or a substring from an original string by using a range of index numbers separated by a colon in the format [x:y]. Here, the first index number is inclusive and specifies where the slice starts from. The second index number is exclusive and specifies where the slice ends.
We can include either end of a string by omitting one of the index numbers. For example, to create a slice that starts at the beginning of the string and ends in the middle, we have to specify only the index number after the colon as shown below:
Likewise, we can create a slice that starts in the middle of the string and includes each character until the end of the string by specifying only the index number before the colon as shown below:
We can also omit both the index numbers to create a slice that starts at the start of the string and ends at the end of the string. This will be beneficial in combination with the stride parameter discussed below.
It is also possible to use negative index numbers for string slicing.
Specifying Stride While Slicing Strings
In the examples above, we have used two index numbers to slice a string. It is also possible to use a third parameter called 'stride' which specifies how many characters to move forward after the first character has been retrieved from the string. The default value of stride is 1.
A negative value for stride specifies that we want to extract characters from the string in reverse order.