Skip to main content
Version: 7.9

Strings

What are Strings?

In Python, as well as most other programming languages, a string is a grouping or string of characters that are used literally instead of as names of variables or other objects. In Python, they can be enclosed in either single quotes or double quotes:

Python - Print Statements
# These two print statements will both print out a sentence that looks the same as the other.
print 'These will both print the same!'

print "These will both print the same!"

Strings are also considered sequences, which means they can be iterated through much like a list.

String Escape Character

Strings use a special character called an "escape" character to denote when something different should be happening within the string. In Python, the escape character is the backslash ( \ ). The escape character is mainly used in a few different ways:

Comments

Here, we use the escape character to let Python know we want to ignore the second single quote and directly print it. This works with either single or double quotes.

Alternately, we can enclose the string in the opposite quotes that we are using in the string. Because we enclose the string in double quotes here, the single quote prints normally.

Both of the following examples will result in doesn't:

print 'doesn\'t'

print "doesn't"

This also works in reverse, where we can enclose the string in single quotes and use double quotes within. We can also use the escape character with double quotes enclosing the string.

For example, both of the following print statements will result in "Test," he said.:

print '"Test," he said.'

print "\"Test,\" he said."

New Lines and Tab Spacing

A new line may be specified with the escape character and "n". Alternatively, a multi-line string may be used to add line breaks to a string literal.

print "First line\nSecond line"

Results in:

First line

Second line

Tab-spacing may be added to a string literal with the escape character and "t" print "Hello \tWorld" results in Hello World.

Specifying a Backslash

If you need to place a backslash inside of a string literal, simply use the escape character twice.

For example, print "Folder\\file.txt" results in Folder\file.txt.

Multi-Line Strings

If a string literal should contain multiple lines, you can use a multi-line string, which is created by using three quotation marks.

ExampleOutput
print '''This
is
on
multiple
lines'''
This
is
on
multiple
lines

This is useful when using the system.db functions, as full SQL queries are typically passed to these functions, so using a multi-line string allows you to write the query in an easy to read format.

Python - Multi-line String
query = ''' SELECT * FROM myTable
WHERE id < 10'''

system.db.runPrepQuery(query,[])

Raw Strings

Sometimes, it is necessary to print the raw string without allowing escaped characters. This is done by placing the letter "r" in front of the string.

Python - Raw String
print r'This string \n will print out directly as written.'

This is especially useful in cases where a file path needs to be hard coded as a string literal:

Python - File Path as a Raw String
# Specifying a Windows file path
myPath = r"C:\Folder\Another Folder\file.txt"
print myPath

# Specifying a Linux file path
myPath = r"/home/Directory/Another Directory/file.txt"
print myPath

Unicode Strings

Strings that contain characters beyond 7-bit ASCII, such as é or щ need to be marked as unicode strings by placing the letter u in front of the string.

Python - Unicode String
print u'été'

Combining Strings

Two different string type variables can actually be combined or concatenated using the plus ( + ) sign. It is important to understand that they are concatenated exactly.

Python - Concatenated String Type Variables
a = "this"
b = "that"


# Will print 'thisthat' because there was no space at the end of a or the beginning of b.
print a + b

# Will print 'this that' because we added a space between them.
print a + " " + b

String Indexing

Strings in Python are actually indexed, with the first character having an index of 0. To grab a value at a specific index, you place a value within square brackets ( [ ] ) after the string variable.

Python - Indexed Strings
a = "Ignition"

# Will print out 'I', since it is in the zero position.
print a[0]

# Will print out 't', since it is in the fourth position.
print a[4]

You can also use a negative index, which will start from the right side of the string with the last character having a index value of -1. This is useful for getting the last character when you aren't sure how long the string is.

Python - Negative Indexed String
a = "Ignition"

# Will print out 'o', since it is the second to last character.
print a[-2]

Slicing Strings

Using the string index values, we can actually grab slices or parts of the string. Similar to grabbing an individual character, we place two values separated by a colon within square brackets. You can think of these numbers as the slices between characters, with 0 before the first character, 1 before the second, and so on. It should look like this: string[4:7] The first value is the start location, while the second value is the end location.

Python - Sliced Strings
a = "Inductive Automation"

# Will print out 'ctive Auto'. Note how the space is counted as a character,
# and the 14th location is before the m, which is not included.
print a[4:14]

If left blank, the first value will default to 0, while the second value will default to the length of the string. Additionally, negative values can be used as well, just like with the index.

Python - Negative Value Sliced Strings
a = "Inductive Automation"

# From index 0 to index 12.
print a[:12] # Will print out: Inductive Au

# From index 12 to the end of the string.
print a[12:] # Will print out: tomation

# From the 4th character from the end to the 5th index. Note this prints nothing because the start character is after the end character.
print a[-4:5] # Will print out:

# From the 8th character from the end to the 2nd character from the end.
print a[-8:-2] # Will print out: tomati

# From index 5 to the 5th character from the end.
print a[5:-5] # Will print out: tive Autom

You can use all of these together (and with integer variables or functions) to have a very flexible way to format strings.

String Formatting

Also known as string substitution, it allows you to enter in values into a string, similar to having a variable inside of the string. This is useful when doing a database query where you can build the query as a string, and then add in the query parameters when executing the query.

Doing this requires the use of the percent ( % ) sign followed by the parameter type within the string. After the string, you then use the percent sign followed by the parameter values within parentheses in order.

Python - String Substitution
a = "I have %i apples."

# Will print exactly as written, because we are not substituting any values in.
print a

# Will print with an 8 substituted in for the %i. I have 8 apples.
print a % (8)

Instead of using literal values, you can also use variables as parameter values instead. This is useful when pulling the value from a function. Each string can also have multiple values substituted, as long as the parameters are in the order that they are in the string.

Python - String Substitution using Variables as Parameters
apples = 10
oranges = 14
peaches = 5

a = "I have %i apples, %i oranges, and %i peaches."

# Will print with the variable values substituted in for the %i. I have 10 apples, 14 oranges, and 5 peaches.
print a % (apples, oranges, peaches)

There are a few different types used with string formatting, the most common of which are listed here. The character values are what go after the percent sign.

TypeCharacter
Signed Integeri
Floating Point Decimal Formatf
Floating Point Exponential Formate
Strings

String Search Example

You can bring all this together for a simple way to print out just the last word in a string.

Python - Find the Last Word of a String
# Find just the last word of a string.
myString = "Inductive Automation"

lastSpaceIndex = myString.rfind(" ")

# Add one to not include the space.
print myString[lastSpaceIndex+1:] # Will print out: Automation

String Functions

Python strings have many functions available that can manipulate the string or give information on the string. The most common are in the table below.

FunctionDescription                      ExampleOutput
len(string)Returns the length of the string.
a = "Inductive Automation" 
print len(a)
20
x in stringWill return True if x is within the string, False if not. Can also be used to iterate through the string.
string = "Inductive Automation" 

if 'd' in string:
print "There is a d in the string"
for letter in string:
print letter
There is a d in the string
I
n
d
u
c
t
i
...
string.find(x, [, start[, end]])Returns the first location of the substring x from the string. Returns -1 if the substring is not found.
string = "Inductive Automation"
print string.find(" ")
9
string.rfind(x, [, start[, end]])Similar to find, but returns the last location of the substring x. Returns -1 if not found.
string = "Inductive Automation" print string.find("i")
17
string.upper()Returns a copy of the string with all characters uppercase. This is useful when comparing user input to a string value, as the user may use a different case for certain letters.
userInput = "administration" print userInput.upper()
ADMINISTRATION
string.lower()Returns a copy of the string with all characters lowercase. This is useful when comparing user input to a string value, as the user may use a different case for certain letters.
serInput = "ADminIStration" print userInput.lower()
administration
string.capitalize()Returns a copy of the string with the first character capitalized and all other characters lowercase.
string = "here is my sentence." print string.capitalize()
Here is my sentence.
string.title()Returns a copy of the string with the first letter of each word capitalized and all other characters lowercase.
string = "here is my sentence." print string.title()
Here Is My Sentence.
string.strip([x])Returns a copy of the string with leading and trailing characters removed, where x is the string of characters.. If x is omitted, then removes whitespace from the leading and trailing edges of the string. lstrip() and rstrip() may instead be used to strip characters from the leading or trailing edge
string = "    This string has some empty space"
print string
print string.strip()
fencedString = "+++++My String+++++"
print fencedString.strip("+")
print fencedString.lstrip("+
")
print fencedString.rstrip("+")
     This string has some empty space
This string has some empty space
My String
My String+++++
+++++My String
string.count(x[, start[, end]])Returns the number of occurrences of x in the string. A start and end can be specified that will limit the count to that area.
string ="Inductive Automation"
print string.count('i')
print string.count('i', 4, 9)
2
1
string.split([delimiter[,maxsplit]])Returns a list of the words in the string. Optionally, specifying a delimiter will split the string on the delimiter string. Specifying a maxsplit will split the string a maximum number of times, with the remainder of the string as the final list object. The number of items in the list will not be more than maxsplit + 1
sentence = "This is an example of split"
print sentence.split()
dashedSentence = "Yet-another-sentence-here"
print dashedSentence.split("-",2)
['This', 'is', 'an', 'example', 'of', 'split']
['Yet', 'another', 'sentence-here']
string.rsplit([delimiter[,maxsplit]])Similar to split, but splitting is performed from right to left. Thus if maxsplit is smaller than the total number of delimiter in the string, only the rightmost words will be split off as separate items.
sentence = "This is an example of split"
print sentence.rsplit()
dashedSentence = "Yet-another-sentence-here"
print dashedSentence.rsplit("-",2)
['This', 'is', 'an', 'example', 'of', 'split']
['Yet-another', 'sentence', 'here']
string.join(x)Returns a copy of the string that is the concatenation of the strings in the iterable x. The string calling the join is what will separate the values of the iterable.
a = '-'
b = 'abcdef'
print a.join(b)
a-b-c-d-e-f
string.replace(old, new[, count])Returns a copy of the string where occurrences of the old substring are replaced with the new substring. Optionally, if count is specified, will only replace the first count number of occurrences.
string ="Ignition is good!"
print string.replace("good", "awesome")
Ignition is awesome! 

:::