SPSS tutorials website header logo SPSS TUTORIALS VIDEO COURSE BASICS ANOVA REGRESSION FACTOR

Python Text Replacements Tutorial & Examples

Summary

A handy technique in Python are text replacements: we insert tiny pieces of text such as variable names into a larger piece of text such as an SPSS syntax command.

In this tutorial, we'll simply build and print SPSS syntax. Readers who also want to run the Python generated syntax may use health-costs.sav, partly shown below.

SPSS Health Costs Variable View

Single Text Replacement with Concatenation

Before diving into text replacements, I'd like to point out that you could perform them with a simple string concatenation. Our first example shows how it's done.

*SINGLE TEXT REPLACEMENT BY CONCATENATION.

begin program python3.
xVars = ['alco','cigs','exer']
for xVar in xVars:
    print("GRAPH/SCATTER " + xVar + " WITH COSTS.")
end program.

Note that we specified some SPSS variable names as a Python list holding quoted strings. A nicer option for doing so is expanding the SPSS TO keyword. We'll show how to make this work in Looping over SPSS Commands with Python.

Result

Python Generated SPSS Syntax In Output Window

Ok, so our concatenation works. However, it's pretty cumbersome, especially for multiple text replacements. So let's now try something more elegant.

Single Text Replacement with Curly Brackets

For a very basic text replacement, just enter a pair of curly brackets into some Python string. After closing off your string, add .format() with some text replacement between the parentheses.

*SINGLE TEXT REPLACEMENT WITH CURLY BRACKETS.

begin program python3.
xVars = ['alco','cigs','exer']
for xVar in xVars:
    print("GRAPH/SCATTER {} WITH COSTS.".format(xVar))
end program.

Note that this code is simpler and more readable than our first example. However, it creates the exact same SPSS syntax. The figure below clarifies its basic structure.

Python Text Replacement Curly Brackets

Multiple Text Replacements with Curly Brackets

Let's now add some more flexibility to our SPSS scatterplot syntax: we'll also allow for multiple y-variables and a (sub)title.

Since the required SPSS syntax becomes longer, it's wise (but not strictly necessary) to use multiple lines for it. The example below does just that by enclosing the basic GRAPH command in triple single quotes.

*MULTIPLE TEXT REPLACEMENTS WITH CURLY BRACKETS.

begin program python3.
xVars = ['alco','cigs','exer']
yVar = 'costs'
subTitle = 'All Respondents | N = 525'
for xVar in xVars:
    print('''
GRAPH/SCATTER {} WITH {}
/TITLE 'MY SCATTERPLOT'
/SUBTITLE '{}'.'''.format(xVar,yVar,subTitle)
)
end program.

Note that the curly brackets occur 3 times now. By default, the first pair is replaced by the first element specified after format and so on.

Result

Python Text Replacements Long Syntax In SPSS Output Window

Rather than simply using empty curly brackets, we can be more specific regarding what they're to be replaced with: in the example below, we'll add indices to them which refer to the elements in the tuple specified by format. Like so, we can choose in which order these replacements are inserted into our string object.

*MULTIPLE TEXT REPLACEMENTS WITH INDICES.

begin program python3.
xVars = ['alco','cigs','exer']
yVar = 'costs'
subTitle = 'All Respondents | N = 525'
for xVar in xVars:
    print('''
GRAPH/SCATTER {2} WITH {1}
/TITLE 'MY SCATTERPLOT'
/SUBTITLE '{0}'.'''.format(subTitle,yVar,xVar)
)
end program.

Perhaps even better than indices, we can also choose names for our placeholders. These may or may not correspond to Python object that have already been defined.

*MULTIPLE TEXT REPLACEMENTS WITH NAMES.

begin program python3.
xVars = ['alco','cigs','exer']
yVar = 'costs'
subTitle = 'All Respondents | N = 525'
for xVar in xVars:
    print('''
GRAPH/SCATTER {x} WITH {y}
/TITLE 'MY SCATTERPLOT'
/SUBTITLE '{subTitle}'.'''.format(subTitle = subTitle,y = yVar,x = xVar)
)
end program.

Regarding this syntax, note that

Text Replacements with F-Strings

Python 3.6 (available for SPSS 27 or higher) introduces f-strings: we can use previously defined Python objects as placeholder names with or without any further specification if we precede a string with “f” (for format).

*MULTIPLE TEXT REPLACEMENTS WITH F-STRING.

begin program python3.
xVars = ['alco','cigs','exer']
yVar = 'costs'
subTitle = 'All Respondents | N = 525'
for xVar in xVars:
    print(f'''
GRAPH/SCATTER {xVar} WITH {yVar}
/TITLE 'MY SCATTERPLOT'
/SUBTITLE '{subTitle}'.'''
)
end program.

Note that this works quite similarly to using locals().

If desired, you can combine this with raw strings which basically disable escaping with backslashes.

*RAW F-STRING EXAMPLE.

begin program python3.
subFolder = 'ruben'
print(rf'd:\data\new file\{subFolder}\here')
end program.

Multiple Text Replacements with Percent Signs

An older method for text replacements in Python is using escape sequences indicated by percent signs:

After specifying some string, specify your text replacements as a tuple preceded by a percent sign.

*MULTIPLE TEXT REPLACEMENTS WITH %.

begin program python3.
xVars = ['alco','cigs','exer']
yVar = 'costs'
subTitle = 'All Respondents | N = 525'
for xVar in xVars:
    print('''
GRAPH/SCATTER %s WITH %s
/TITLE 'MY SCATTERPLOT'
/SUBTITLE '%s'.'''%(xVar,yVar,subTitle)
)
end program.
Python Text Replacement With Locals

Somewhat surprisingly, %s usually works fine for numbers as well as strings.

Multiple Text Replacements with locals()

As previously discussed, you can directly insert Python objects into strings by using f-strings. An older method that works similary is locals(). The syntax below shows how it works.

*MULTIPLE TEXT REPLACEMENTS WITH LOCALS().

begin program python3.
xVars = ['alco','cigs','exer']
yVar = 'costs'
subTitle = 'All Respondents | N = 525'
for xVar in xVars:
    print('''
GRAPH/SCATTER %(xVar)s WITH %(yVar)s
/TITLE 'MY SCATTERPLOT'
/SUBTITLE '%(subTitle)s'.'''%locals()
)
end program.

Note that %(xVar)s is a placeholder for the (already defined) xVar string object. Adding %locals() after defining some string confirms that all placeholders are to be replaced by local variables: Python objects with the same names that were previously defined.

Before closing off, I should point out that the text replacements discussed in this tutorial are known as Python string formatting. And there's way more to it than I discussed here.

Fortunately, we won't need more than just the basics for getting things done with Python in SPSS.

So I hope you found this helpful.

Thanks for reading!

Tell us what you think!

*Required field. Your comment will show up after approval from a moderator.

THIS TUTORIAL HAS 1 COMMENT: