Python Strings

When working with a string of characters, Python has a number of built in functions and methods that allow for them to be easily analysed and manipulated.

When working with the console, a string can be displayed using the ‘print’ function.

print("Hello world!")

If it is necessary to incorporate the value of a variable into this message it can be done in a number of different ways.

fname = "Bob"

print("Hello " + fname + "!")
print("Hello {}!".format(fname))
print(f"Hello {fname}!")

All three of the above examples display the same message in the console, ‘Hello Bob!’. The first example uses the ‘+’ symbol to concatenate the variable into the string. The second example uses the curly braces as a placeholder for the variable that has been specified after the comma. Note that if it is necessary to incorporate more than one variable into the string, additional curly braces can be used and the two variables to incorporate would be separated by a comma.

fname = "Bob"
age = 30

print("{} is {} years old.".format(fname, age))

The third method of incorporating variables into a string is known as an ‘f-string’. It uses an ‘f’ before the opening double quote and then the variables to be included are enclosed in curly braces. It should be noted that this last method is only available in Python 3.6 onwards.

Index Value

Every character in a string has an index value, which can be used to reference it. The first character has an index value of zero, the second has an index of one and so on.

example = "This is a string."


Here, the index value of three in square brackets is used to output the fourth character of the string to the console.

It is also possible to find the index position of a character in a string using the ‘find’ method.

example = "This is a string."


Where there are two instances of the character, in this case ‘i’, the index position of the first instance is returned. If the character doesn’t exist at all, then a ‘-1’ is returned. It is also possible to search for a string of characters in another string using this method.

example = "This is a string."


In this case the index position returned is that of the first character in the search string.

If it is necessary to search for a character, or string, within part of another string, then two further arguments can be passed to the ‘find’ method, the index position to start and end the search.

example = "This is a string."

print(example.find("string", 0, 9))

In this instance, the word ‘string’ does not exist in the first ten characters of the ‘example’ variable, so a ‘-1’ is returned. Where the end index value is omitted, the ‘find’ method goes to the end of the string. Where both the start and end index positions are omitted, as in the first two examples, the ‘find’ method searches right from the start of the string to the end.


The index values of a string can be used to return a portion of a string. Here, a start index position of ten and an end index position of sixteen is used to return the word ‘string’ from the variable.

example = "This is a string."


This is known as a ‘slice’ and is discussed in more detail here.

Upper and Lower Case

In order to change a string to upper or lower case, there are two methods that can be used, ‘upper’ and ‘lower’.

example = "This is a string."


The first will change the whole string to upper case and the latter will convert it to lower case.

this is a string.


If a string has extra spacing at the start, end, or both, it’s possible to remove this using the methods ‘lstrip’, ‘rstrip’ and ‘strip’, to remove the extra spacing from the start, end or both respectively. An asterisk has been concatenated on to both ends in these examples to demonstrate the removal of the white space.

example = "   This is a string.   "

print("*" + example.lstrip() + "*")
print("*" + example.rstrip() + "*")
print("*" + example.strip() + "*")

The resulting output from the above is shown below.

*This is a string.   *
*   This is a string.*
*This is a string.*


Sometimes it is necessary to find the size or length of a string. This can be done using the ‘len’ function.

example = "This is a string."



As its name suggests, the ‘replace’ method can be used to replace a character, or string of characters, within a string.

example = "This is a string."

print(example.replace("is", "was"))

Notice that it replaces two instances of ‘is’, with the first being part of the word ‘This’.

Thwas was a string.

A third argument can be passed to the ‘replace’ method to limit the number of instances that it replaces.

example = "This is a string."

print(example.replace("is", "was", 1))

Here, only the first instance of the search term is replaced because one has been specified as the third argument.

Thwas is a string.

String Analysis Methods

Python provides a number of methods to analyse strings. These methods return a boolean value, true or false, and can be used in ‘if’ statements to make decisions within a program. Below is a list of some of these methods, along with a short description for each.

Method Description
isalnum Checks if a string contains only alphanumeric characters.
isalpha Checks if a string contains only letters of the alphabet.
isdigit Checks if a string contains only numbers.
istitle Checks if a string is in title case, where the first letter of every word is a capital.
isupper Checks if all the letters of the alphabet in the string are upper case.
islower Checks if all the letters of the alphabet in the string are lower case.
isspace Checks if the string contains only spaces.
startswith Checks if the string starts with the specified letter or string.
endswith Checks if the string ends with the specified letter or string.

The following examples display the result for each of these methods to the console, using a variable called ‘example’, that contains the string, ‘This is a string.’.

example = "This is a string."


As can be seen from the resulting output, the only two methods that return true are ‘startswith’ and ‘endswith’. The full stop and spaces prevent true from being returned with the ‘isalnum’ and ‘isalpha’ methods.