Overview Python String Methods
An important part of any programming language are string manipulations. In Python, these are known as string methods. The table below gives a quick overview.
Overview Python String Methods
WHAT | PYTHON | Returns | PYTHON EXAMPLE | SPSS EXAMPLE |
---|---|---|---|---|
Extract Substring | [] | String | myString[0] | compute str01 = char.substr(str01,1,1). |
Concatenate 2(+) Strings | + or += | String | myString + myString | compute str01 = concat(str01,str02). |
Find Leftmost Occurrence of Substring | find | Integer | myString.find('a') | compute pos = char.index(str01,'a'). |
Find Rightmost Occurrence of Substring | rfind | Integer | myString.rfind('a') | compute pos = char.rindex(str01,'a'). |
Replace 1(+) Characters | replace | String | myString.replace('a','b') | compute str01 = replace(str01,'a','b'). |
Find Length of String | len | Integer | len(myString) | compute len01 = char.length(str01). |
Lowercase String | lower | String | myString.lower() | compute str01 = lower(str01). |
Uppercase String | upper | String | myString.upper() | compute str01 = upper(str01). |
Capitalize String | capitalize | String | myString.capitalize() | (None) |
Remove Characters from Left Part of String | lstrip() | String | myString.lstrip() | compute str01 = ltrim(str01). |
Remove Characters from Right Part of String | rstrip() | String | myString.rstrip() | compute str01 = rtrim(str01). |
Remove Characters from Left and Right Part of String | strip() | String | myString.strip() | (None) |
Convert String to Integer | int | Integer | int(myString) | compute num01 = number(str01,comma16). (Or use ALTER TYPE.) |
Split String into Python List | split | List | myString.split(' ') | (None) |
Check if String Starts With... | startswith | Boolean | myString.startswith("var") | (None) |
Check if String Ends With... | endswith | Boolean | myString.endswith("var") | (None) |
Left Pad String with Zeroes | zfill | String | myString.zfill(3) | compute str01 = char.lpad(str01,3). |
Extract Substring in Python
We extract substrings in Python with square brackets that may contain one or two indices and a colon. Like so,
myString[0]
extracts the first character;myString[1:]
extracts the second through last characters;myString[:4]
extracts the first through fourth characters;myString[1:3]
extracts the second through third characters;myString[-1]
extracts the last character.
Python Substring Examples
begin program python3.
myString = 'abcdefghij'
print(myString[0]) # a
print(myString[1:]) # bcdefghij
print(myString[:4])# abcd
print(myString[1:3]) # bc
print(myString[-1]) # j
end program.
Concatenating Strings in Python
Basically, + concatenates two or more strings. Additionally, myString += 'a' is a nice shorthand for myString = myString + 'a' that we'll often use for building SPSS syntax.
Python Concatenate Examples
begin program python3.
myString = 'abc'
print(myString + 'def') #abcdef
end program.
*2. CONCATENATE WITH "+="
begin program python3.
myString = 'abc'
for i in range(5):
myString += str(i)
print(myString) #abc01234
end program.
Note: in these examples, we're technically creating new string objects rather than truly changing existing string objects. This is because strings are immutable in Python.
Find Leftmost Occurrence of Substring
Retrieving positions for single or multiple character substrings is done in Python with find
. Keep in mind here that
- Python is fully case sensitive and
- Python objects are zero-indexed.
Like so, the indices for the characters in our examples are shown below.

Python Find Examples
begin program python3.
myString = 'Cycling in the mountains is fun.'
print(myString.find('c')) # 2
print(myString.find('in')) # 4
end program.
Find Rightmost Occurrence of Substring
In Python, rfind
returns the index (again, starting from zero) for the rightmost occurrence of some substring in a string. The syntax below shows a couple of examples.
begin program python3.
myString = 'Cycling in the mountains is fun.'
print(myString.rfind('i')) # 25
print(myString.rfind('in')) # 21
end program.
Replacing Characters in a Python String
Replacing characters in a string is done with replace
in Python as shown below.
begin program python3.
myString = 'The cat caught the mouse in the living room.'
print(myString.replace('a','')) #The ct cught the mouse in the living room.
print(myString.replace('the','a')) # The cat caught a mouse in a living room.
end program.
Note: in line 5 we replace all a’s with an empty string. That is, we'll remove all a’s from our example sentence.
Find Length of Python String
In Python, len
returns the number of characters (not bytes) of some string object.
begin program python3.
myString = 'abcde'
print(len(myString)) # 5
end program.
Convert Python String to Lowercase
For converting a Python string to lowercase, use lower
as shown below.
begin program python3.
myString = 'SPSS Is Fun!'
print(myString.lower()) # spss is fun!
end program.
Convert Python String to Uppercase
In Python, upper
converts a string object to uppercase.
begin program python3.
myString = 'This is Some Title'
print(myString.upper()) # THIS IS SOME TITLE
end program.
Capitalize Python String Object
In Python, “capitalizing” means returning a string with its first character in uppercase and all other characters in lowercase -even if they were uppercase in the original string.
begin program python3.
myString = 'aBcDeF'
print(myString.capitalize()) # Abcdef
end program.
Remove Characters from Left Part of String
In Python, just lstrip()
removes all spaces and tabs from the beginning of a string. Any other leading character can be removed by specifying it within the parentheses (line 12 below).
begin program python3.
myString = ' left padding removed'
print(myString.lstrip()) # left padding removed
end program.
*REMOVE ASTERISKS (*) FROM START OF STRING.
begin program python3.
myString = '****left padding removed'
print(myString.lstrip('*')) # left padding removed
end program.
Remove Characters from Right Part of String
The rstrip
method works the same as lstrip but removes characters from the right side of some string.
begin program python3.
myString = 'right padding removed '
print(myString.rstrip()) # right padding removed
end program.
*REMOVE ASTERISKS (*) FROM END OF STRING.
begin program python3.
myString = 'right padding removed****'
print(myString.rstrip('*')) # right padding removed
end program.
Remove Characters from Left and Right Part of String
Just strip
basically combines the Python lstrip and rstrip methods.
begin program python3.
myString = ' left and right padding removed '
print(myString.strip()) # left and right padding removed
end program.
*REMOVE ASTERISKS (*) FROM END OF STRING.
begin program python3.
myString = '****left and right padding removed****'
print(myString.rstrip('*')) # left and right padding removed
end program.
Sadly, this method doesn't have an SPSS equivalent, which is why we sometimes see LSTRIP(RSTRIP(MYSTRING)) in older syntax. Note that whitespace is often stripped automatically from string values in SPSS Unicode mode.
Convert String to Integer
In Python, int
converts a string to an integer. If a string contains anything else than digits, it'll crash with an error.
begin program.
myString = '123'
myInt = int(myString)
print(type(myInt)) # <type 'int'>
print(myInt) # 123
end program.
Split Python String into List Object
The example below splits a string into a Python list object. split
always requires some separator. Splitting a string without any separator can be done with a list comprehension (line 14 below).
begin program python3.
myString = 'A A C A B C'
myList = myString.split(' ')
print(type(myList)) # <type 'list'>
print(myList) # ['A', 'A', 'C', 'A', 'B', 'C']
end program.
*SPLIT STRING INTO PYTHON LIST WITHOUT SEPARATOR.
begin program python3.
myString = 'AACABC'
myList = [i for i in myString]
print(myList) # ['A', 'A', 'C', 'A', 'B', 'C']
end program.
Check if String Starts With...
begin program python3.
myString = 'abcdef'
print(myString.startswith('abc')) # True
print(myString.startswith('bcd')) # False
end program.
*TYPICAL USE OF STARTSWITH().
begin program python3.
if myString.startswith('a'):
print("First character is 'a'.")
else:
print("First character is not 'a'.")
end program.
Note: True and False are the (only) 2 possible values for Booleans. We mostly use them when we only want to run one or Python if statements.
Check if String Ends With...
begin program python3.
myString = 'abcdef'
print(myString.endswith('f')) # True
print(myString.endswith('e')) # False
end program.
Left Pad String with Zeroes
In Python, zfill(3)
left pads a string with zeroes up to a total length of 3 characters. We mostly do so when we want to sort numbers alphabetically: 002 comes before 010 and so on.
begin program python3.
myString = '1'
print(myString.zfill(3)) # 001
myString = '10'
print(myString.zfill(3)) # 010
end program.
So that's about it for Python string methods. I hope you found this tutorial helpful.
Thanks for reading!
Python Text Replacements Tutorial & Examples
- Single Text Replacement with Curly Brackets
- Multiple Text Replacements with Curly Brackets
- Text Replacements with F-Strings
- Multiple Text Replacements with Percent Signs
- Multiple Text Replacements with locals()
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.

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.
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

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.
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.

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.
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

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.
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.
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
- placeholder {x} is replaced by xVar;
- placeholder {y} is replaced by yVar;
- placeholder {subTitle} is replaced by subTitle.
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).
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.
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:
- use
%s
as a placeholder for strings; - use
%d
as a placeholder for integers; - use
%f
as a placeholder for floats.
After specifying some string, specify your text replacements as a tuple preceded by a percent sign.
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.

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.
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!