1. What are common ways to manipulate strings?
The next topic we'll cover is string manipulation. Coding exercises in interviews often involve this subject.
2. String
Strings are created simply by enclosing a text
in single
or double quotes.
3. String
Generally, strings are created using the str() constructor. We can also pass to it other data types
like real numbers,
or lists to convert them to a string.
4. str() constructor
Actually, we can pass any object to the str() constructor. Let's check it with our own object.
Recall that first we need to create a class representing a "blueprint" of an object.
We need to include the __init__() method indicating the state of our object at initialization. Here, an object is initialized with the num variable.
After creating an instance, we can retrieve the value of this variable.
If we pass the object to the str() constructor, we'll get quite unreadable output. How to customize it?
5. str() constructor
We have to implement
the __str__() method in the class. Here, this method will return the value of the num variable.
Now, when we create an instance
and pass it to the str() constructor, we get the number defined at initialization.
6. Accessing characters in a string
String characters are indexed.
Therefore, we can access each character using square brackets with the corresponding index.
Negative indices can be used as well.
We can also use slicing.
If left-hand or right-hand side index is omitted, all the characters in the corresponding direction are considered.
7. The .index() method
To retrieve an index of a specific character in a string,
use the .index() method.
Note that if a character is present in a string more than once, only the lowest index is returned.
8. Strings are immutable
Strings are immutable. We cannot modify an existing string.
Doing so will raise an error. You could ask:
but there are plenty of methods that we can apply on a string object implying modification! The answer is: it only looks like modification.
In reality, we return a new string object. Let's look at some of these methods.
9. Modifying methods 1
First is
string concatenation. It is easily done with the "+" operator.
Another important method is replacing a substring. It substitutes all the occurrences of a specific character sequence in a string with another sequence.
10. Modifying methods 2
There is a set of methods to change case representation in a string. For example,
transforming to the upper case
or to the lower case. If we need to capitalize only the first letter in a string,
we have to use the .capitalize() method.
11. Relation to lists
Let's look at a couple of methods dealing with lists.
We will begin with creating a string from a list of strings.
Assume we have the following list.
To convert it to a proper sentence, we can use the .join() method. We need to specify a delimiter that is inserted between the strings in the list and to pass the list as an argument.
The second method is about breaking a string into a list. Let's inverse the operation we just did.
For this, we can use the .split() method. We need to specify the string to apply the method to and the delimiter for splitting.
12. String methods with DataFrames
Knowledge of string methods is very handy because we can use them with DataFrames. More specifically, we can apply them to columns containing text data.
Let's create a DataFrame using a custom dictionary. As you can see, the name column contains text data. However, the names are not capitalized.
13. String methods with DataFrames
To change that, we need to modify the column of interest.
14. String methods with DataFrames
We start by accessing the column.
15. String methods with DataFrames
Then, we add an str specifier that gives us access to the string methods.
16. String methods with DataFrames
And finally, we call the .capitalize() method we already know.
Let's check the result! Great! We capitalized all the names in our DataFrame in just seconds!
17. Let's practice!
That was quite a lot of information. Let's put it all together and get some practice!