In the previous section, we viewed vector layer fields and attributes. Now let’s use these attributes to put a filter on our layer so that it only contains certain features.

For example, this is how we can filter our countries vector layer to only features where the ADMIN value starts with an A:

vlayer.setSubsetString("ADMIN LIKE 'A%'")
for feature in vlayer.getFeatures():

This code is the equivalent of going to the layer properties and entering ADMIN LIKE ‘A%’ in the provider feature filter.

To remove the filter, we set an empty subset string:

for feature in vlayer.getFeatures():

To make our code more flexible, we can use a variable to define the character we want to filter for. The advantage of using a variable is that we only need to define it once and then we can use it in multiple places within our code. And if we eventually want to change it to a different character, we only have to change it in one place. The following code shows two different approaches from inserting a variable into a string:

my_char = "C"
vlayer.setSubsetString("ADMIN LIKE '"+my_char+"%'")
print("The following country names start with {}:".format(my_char))
for feature in vlayer.getFeatures():

The first approach is to concatenate strings using + operators. The second approach is called string formatting and the above syntax (with curly brackets) is specific to Python 3 (and thus requires QGIS 3).

String formatting looks complicated at first sight but it is also really powerful. For example, we can output the number of people living in each country using:

for feature in vlayer.getFeatures():
    print("{pop:.2f} mio people live in {name}".format(name=feature['ADMIN'],pop=feature['POP_EST']/1000000))

4.51 mio people live in Central African Republic
33.49 mio people live in Canada
16.60 mio people live in Chile

This is interesting for multiple reasons: first, the parameters in the format function can be provided in any order because we assigned them names (pop and name in this example). Second, we formatted the population value to display two digits after the decimal separator. For more string formatting examples see the official Python documentation.

This is one way to filter the features in a vector layer. You also saw some different methods for integrating variables into strings using either the + operator or string formatting.


PyQGIS 101 is a work in progress. I’d appreciate any feedback, particularly from beginners!

  1. Kurt said:

    my_char = “C”
    vlayer.setSubsetString(“ADMIN LIKE ‘”+my_char+”%'”)
    print(“The following country names start with {}:”.format(my_char))
    for feature in vlayer.getFeatures():

    why does replacing the first line of code from above with
    my_char = input(“Enter Char”)
    not work?

    • I assume input(“Enter Char”) is an attempt to make the script interactive but this is not the way to achieve interactivity in PyQGIS. I’ll definitely get into this topic later.

  2. Hi, I can’t differentiate the “two approaches” that you mention above. Can you please be more specific?

    Thanks, I’m learning a lot!

    • Approach #1 is string concatenation using + operators:
      "ADMIN LIKE '"+my_char+"%'"

      Approach #2 is string formatting:
      "ADMIN LIKE '{}%'".format(my_char)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: