1.4. Strings#

A string in programming is a sequence of characters and is used to handle text data. A string in Python is declared using single or double quotation marks, so 'hello world' is the same as "hello world".

Declaring a string variable is simply done by assigning a variable equal to a string.

variable = "string"

For example, enter the following into the console.

In [30]: string = "hello world"

To output a string we can use the print() command (printing is explained in more detail here).

print(variable)

Lets print our string variable, enter the following into the console

In [31]: print(string)
Out[31]: hello world

To define a multiline string we can use triple single or double quotation marks. For example, enter the following into the console1 (you will need to press the enter key after the word velocity to continue onto the next line).

In [31]: multiline_string = """What is the air-speed velocity 
    ...: of an unladen swallow?"""

In [32]: print(multiline_string)
What is the air-speed velocity 
of an unladen swallow?

1.4.1. Modifying strings#

Python has the following built-in functions that can be used to modify a string.

Table 1.2 String modification functions#

Function

Description

string.upper()

Converts the characters of a string to uppercase

string.lower()

Converts the characters of a string to lowercase

string.strip()

Remove spaces before and after the characters in a string

string.replace(<old string>, <replacement string>)

Replaces a string with another string

To demonstrate these enter the following code into the console.

In [33]: string = "   Hello World   "

In [33]: print(string.upper())
   HELLO WORLD   

In [32]: print(string.lower())
   hello world   

In [33]: print(string.strip())
Hello World

In [34]: print(string.replace("l", "x"))
   Hexxo Worxd   

1.4.2. Concatenating strings#

To concatenate (merge) two or more strings we use the + operator.

merged_string = string1 + string2 

To demonstrate this enter the following into the console.

In [35]: string1 = "hello"

In [36]: string2 = "world"

In [37]: merged_string = string1 + " " + string2

In [38]: print(merged_string)
hello world

Note that we needed to include a space " " when concatenating the two words, if we didn’t do this the concatenated string would be helloworld.


1.4.3. Indexing characters in a string#

The characters in a string can be indexed using the character position starting at 0 for the first characeter.

string[ index ]

To demonstrate this enter the following into the console.

In [39]: string = "What have the Romans ever done for us?"

In [40]: print(string[0])
W

In [41]: print(string[10])
t

Here we have printed the 1st and 11th character in string.

To index a range of characters in a string we use a colon to separate the first and last characters in the range.

string[ first_character_index : last_character_index + 1 ]

To demonstrate this enter the following into the console.

In [42]: print(string[14:25])
Romans ever

Here we have printed the string which consists of the 15th to the 25th character in string.

Note

A Python string is actually an array of characters so we can use array slicing commands which are covered later to index strings.


1.4.4. Length of a string#

The length of a string is the number of characters in the string and can be determined using the len() function.

len(string)

To demonstrate this enter the following into the console.

In [43]: print(len(string))
38

So our string is 38 characters long.


1.4.5. Exercise#

Exercise 1.3

Define two string variables for the following:

  • String 1: “Your mother was a hamster”

  • String 2: “and your father smelt of elderberries!”

Use your strings to answer the following:

  1. Print string 1 using all lowercase characters

  2. Print string 2 using all uppercase characters

  3. Print string 2 with the word “elderberries” replaced with “roses”

  4. Create another string variable by concatenating string 1 and string 2 and print it

  5. Print the length of your concatenated string

  6. Print the last 30 characters of the concatenated string


1

This is a quote from the film Monty Python and the Holy Grail by the comedy group Monty Python. The creator of Python, Guido van Rossum, was looking for a short unique name and decided to name Python after Monty Python.