Strings
Resources
- Official documentation for the str class.
- Google string tutorial, available in different languages.
- Real Python string tutorial.
- Official string formatting documentation.
- Real Python string formatting tutorial.
String literals
In most programming laguages the type used for text is called strings; a text is a string of characters. In Python you can create an object of type str by using a string literal, like:
String literals can be defined with single and double quotes.
It is also common to use triple quotes. The advantage of the triple quotes is that they can include string literals with several lines.
Fix the following code:
String methods
The string type, through its methods, has a rich functionality available. We can, for instance create upper- or lowercase versions of the strings. This funcionality is accessed via the string class methods. It is still too early to formally introduce the concept what is a method, so lets just show some examples.
So, methods are used by writing a dot (.), the name of the method and two parentheses.
You can ask for the whole documentation of any type, class or function using the help function.
Let’s see some examples of the most commonly used string methods.
We can also split a string into a list of strings.
If we have a list of strings we can join them.
We can also get every letter by using list.
To find out the lenght of a string we use the len function.
Calculate the GC content, percentage of Gs and Cs of a DNA sequence.
You can remove characters using replace(“character”, ““).
You can count the letters with the count method.
We are writting some software that deal with file names, and we would like to change every .txt file into a .doc file. How could we change these file names?
We have file with values separated by commas and we want them separated by tabs, how could we do it.
The tab character is represented by “ and end of line by”“.
Imagine that we are reading a text file in which each line gives us information from one person. Remove the line feed and split the information items.
Remember that you can remove the feed line and return characters using the strip method and that you can split a sting with the split method.
Once you have the three items you can do:
Type casting
Strings can be converted into numbers and numbers into strings.
Fix the following code:
How many digits a number has.
Indexing
Strings are sequences of characters and they can be indexed; we can get some of the characters, by using the square brackets: [].
We will see that this is a general property of any sequence type in Python, all of them can be indexed in the same way.
Indexes also allow us to ask for not just one, but several items. In the string case we will get a string with several characters. To get them we use the colon (:) to separate the first and the last position. Take into account that the first position will be included in the selection but the last one will be not.
Predict the result of running the following code:
The combination of the start and end position is called slice. By using this kind of indexing we are slicing the string. Slices can also be built using the slice built-in function, but this is a more avanced topic.
Slices, besides start and stop, allow for a third number, the step, that indicates the increment between the index of the selected items. For instance, we could use the step to select every three characters.
Step is commonly used to reverse the string by using a step of -1.
Another way of reversing a string, or any Python sequence, is to use the reversed function. The result won’t be exactly the same because reversed returns an iterator that has to be converted into a string. The reversed function could seem more cumbersome, but iterators have many advantages in different contexts. But we won’t discuss them here because this is a more advanced topic.
Excercise, create a way of checking if a string is a palindrome.
You could check if the reversed string is equal to the original string.
Write a program that given a number prints its digits in reverse order and separated by spaces.
Another useful string method is find. It allows us to look for ocurrences of a substring inside of a string. (Remember that we count from zero.)
Find the position of the substring “wor” in the string “Hello world”, and then use that position to extract the “world” substring from “Hello world”
Operations with text strings
Text strings can be added.
One confusing Python behaviour is that it will add two string even if there is no + sign between them. This is called string concatenation.
Predict the result of running the following code:
The multiplication operation is also defined for str and int. It just repeats the string the given number of times.
Write a program that given a number n returns the value of the operation: n + nn + nnn
We could check if a string is included in another string by using the in operator.
Fix the following code:
Remember that variables name can not have spaces.
The method capitalize creates a string with the first letter in uppercase.
Fix the following code:
You can create a string from a number by using the str function.
f-strings
f-strings are a very common and powerful way of creating strings. You will see them a lot in Python code. They allow, for instance to create a string from a variable in a very suscint way: using curly braces ({}).
f-strings have a very powerfull formatting language that allows to specify how the strings should be created.
UTF and the enconding tables
Python strings a UTF-8 strings. String encoding is a more advanced topic, but any seasoned programmer will end up having to understand a little bit about character encoding standards.