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:
# 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:
The Escape Character and Quotation Marks - "" and ''​
Comments | Example | Output |
---|---|---|
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. |
| doesn't |
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. |
| doesn't |
This also works in reverse, where we can enclose the string in single quotes and use double quotes within. |
| "Test", he said. |
Of course, we can also use the escape character with double quotes enclosing the string. |
| "Test", he said. |
New Lines and Tab Spacing​
Comments | Example | Output |
---|---|---|
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. |
| First line Second line |
Tab-spacing may be added to a string literal with the escape character and "t" |
| Hello World |
Specifying a Backslash​
Comments | Example | Output |
---|---|---|
If you need to place a backslash inside of a string literal, simply use the escape character twice. |
| 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.
Example | Output |
---|---|
| 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.
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.
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:
# 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. Implementing unicode can be additionally useful when working with system functions as some functions will fail if tags paths include special characters.
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.
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.
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.
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.
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.
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.
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.
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.
Type | Character |
---|---|
Signed Integer | i |
Floating Point Decimal Format | f |
Floating Point Exponential Format | e |
String | s |
String Search Example​
You can bring all this together for a simple way to print out just the last word in 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.
Function | Description                      | Example | Output |
---|---|---|---|
len(string) | Returns the length of the string. |
| 20 |
x in string | Will return True if x is within the string, False if not. Can also be used to iterate through the string. |
|
|
string.find(x, [, start[, end]]) | Returns the first location of the substring x from the string. Returns -1 if the substring is not found. |
| 9 |
string.rfind(x, [, start[, end]]) | Similar to find, but returns the last location of the substring x. Returns -1 if not found. |
| 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. |
| 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. |
| administration |
string.capitalize() | Returns a copy of the string with the first character capitalized and all other characters lowercase. |
| 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. |
| 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.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.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 |
|
|
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. |
|
|
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. |
|
|
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. |
|
|