Strings

Resources

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:

Note
Note

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.

Note

You can remove characters using replace(“character”, ““).

You can count the letters with the count method.

Note

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?

Tip

We have file with values separated by commas and we want them separated by tabs, how could we do it.

Note

The tab character is represented by “ and end of line by”“.

Note

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.

Note

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:

Note

Type casting

Strings can be converted into numbers and numbers into strings.

Fix the following code:

Note

How many digits a number has.

Note

Indexing

Strings are sequences of characters and they can be indexed; we can get some of the characters, by using the square brackets: [].

Python positive and negative string indexes

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.

Note

You could check if the reversed string is equal to the original string.

Note

Write a program that given a number prints its digits in reverse order and separated by spaces.

Note

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”

Tip

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

Tip

You can transform, type cast, numbers into strings and string into numbers with the functions: int and str.

Tip

We could check if a string is included in another string by using the in operator.

Fix the following code:

Note

Remember that variables name can not have spaces.

The method capitalize creates a string with the first letter in uppercase.

Note

Fix the following code:

Note

You can create a string from a number by using the str function.

Note

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.