Strings in Python

This section will focus on Strings in Python, which are really important because they deal with words and, together with numbers, they are used pretty much everywhere. You can assign a string to a variable similarly to the numeric case (discussed Here) but you need to use quotes or double quotes as shown in the example below.

s='Hello World'
s="Hello World"

In most of the cases using double quotes or single quotes is the same however if a word contains a single quote then use double quotes and vice versa. This is shown below.

s='Hello "World"'
s="Hello 'World'"

We can show the value of a string in the console by using print(). For example we can print the variable $s$.

s='Hello World'
print(s)
Basic Operations

Let's see some operations that can be done on strings. So, string variables can be concatenated by using the "+" symbol. The result of the code below for $s3$ is "HelloValianTek World", there is no space between "Hello" and "ValianTek" because they are simply concatenated. It is possible to contatenate variables with static strings like the space ' ' used in $s4$.

s1='Hello'
s2='ValianTek World'
s3=s1+s2
print(s3)
s4=s1+' '+s2
print(s4)

Python sees Strings as a sequence of characters and the single character can be accessed by using square brackets and the index of the selected character starting from 0. For example, s2[0] outputs "V", but then s2[3] outputs "i". To select a subset of the string it is possible to use colon between the starting index and ending index. However the character in the starting index is included while the ending is not, so s2[0:3] outputs "Val".

s2='ValianTek World'
print(s2[0])   #Prints V
print(s2[3])   #Prints i
print(s2[0:3]) #Prints Val

It is possible to use len(s2) to know the number of characters of a specific string, in this case $s2$. However, it must be said that if the starting index is not included Python assumes that it begins from 0. For example s2[:3] is the same of s2[0:3]. In the same way for the ending index s2[3:] prints from "i" until the end of the string. As it is probably clear, by writing s2[:] Python prints the full string. Another feature of Python is the use negative of indexes which essentially start from the end of the string as shown in the example below.

s2='ValianTek World'
print(s2[:3])  #Prints Val
print(s2[3:])  #Prints ianTek World
print(s2[:])   #Prints ValianTek World
print(s2[-1])  #Prints the last letter which is d
Functions

Once a string variable is created, it is possible to access several functions that are already included within Python. In Spyder, in the console using the variable $s2$ simply add the dot and then press Tab button: as shown in Figure 1, a menu with many pre-built functions will appear and they help you to deal with strings.

Figure 1: Menu with Strings Functions
Figure 1: Menu with Strings Functions

Some examples are:

  • s2.replace(' ','_') replaces every space in the string $s2$ with an underscore, so "ValianTek World" becomes "ValianTek_World".
  • s2.find('T') outputs the index of the first occurrence of the capital T. In this case outputs 6.
  • s2.count('a') outputs how many times the character "a" is found in the string. In this case 2 times.
In all these examples you can change the inputs of these functions to replace or find or count different characters. Also, these functions do not just accept single characters but also multiple characters like strings, or part of strings as inputs. For example s2.count('al') will output 1 given that in this case there is a single occurrence of 'al' within "ValianTek World".

Change of Data Types and Casting

The type of a variable can be found by simply writing "type" and the name of the variable, such as type(s2). This is a string as we know, so it will output "str". However we can re-use the same variable and change also the type:

  • We can write s2=4. Now $s2$ is an "int".
  • We can also write s2=1.5 and now $s2$ is a "float".

Another way of doing this is with Casting. Essentially, we can use the statements int(), float(), str().

  • With int() we convert a variable into an integer. If it is a float the decimal part will be removed for example int(3.2) becomes $3$, if it is a string it needs to represent a whole number, for example int('4') now it becomes the integer $4$.
  • With float() we convert a variable into a float. In particular an integer will get also a decimal part, for example float(5) becomes $5.0$ while the string should represent a number for example float('1.4').

This intro has shown how to use strings, perform basic operations, call the pre-built functions and finally how to change a variable from one type, like string, to another. Try to code them in the console below.

Below you can also see the video of my official Youtube Channel which visually shows these operations in Spyder.