Working with Strings in Python Scripts in NiceForm and NiceLabel Portal

Strings are the basic unit of text in Python. Either single or double quotes can be used to quote strings. This can be useful in some cases:

s1 = "Let's go!"
s2 = '"NiceLabel is the best!", she said.'

 

Special characters need to be escaped. Likely the most common special character you will encounter is the newline ‘\n’. Some of other special characters are:

\\    Backslash (\)
\"    Double quote (")
\'    Single quote (')
\n    ASCII linefeed (LF)
\r    ASCII Carriage Return (CR)
\t    ASCII Horizontal Tab (TAB)

 

In NiceForm version 5.2.3 (that uses Python 2.7) Python string constants are by default ASCII (single byte) strings. Basically this means that they are just a sequence of bytes. If you want to use define Unicode string literal you need to use the ‘u’ or ‘U’ prefix:

s1 = u"čćšđž"
s2 = U"НИГЕЛАБЕЛ"

 

NiceForm variable values are already Unicode enabled so you do not have to do anything. The same goes for scripts running in NiceLabel Portal. Because NiceLabel Portal uses IronPython (that is implemented on .NET Framework) it means that all strings are Unicode strings.

Basic Operations

Strings can be concatenated (glued together) with the + operator, and repeated with *:

s = ""NiceLabel" + " " + "is the best!" # "NiceLabel is the best!"

s2 = "Nice" * 3 # "NiceNiceNice"


To see if two strings are equal use the "==" operator. Other comparison operations are useful for putting words in alphabetical order. You should be aware though that Python does not handle uppercase and lowercase letters the same way people do. All the uppercase letters come before all the lowercase letters.

word = variable.Value
if word == "NiceLabel":
    result.Value = "Your word is NiceLabel."
elif (word < "NiceLabel"):
    result.Value = "Your word comes before NiceLabel."
elif (word > "NiceLabel"):
    result.Value = "Your word comes after NiceLabel."     

 

Sometimes it might be tempting to use the [] operator on the left side of an assignment, with the intention of changing a character in a string. For example:

s = "NiceLabel"
s[0] = 'M'

 

This code will not work because the strings in Python are immutable, which means that you cannot change an existing string. The best you can do is create a new string that is a variation of the original one:

s = "NiceLabel"
s2 = 'M' + s[1:]

 

If you need to count the number of characters in a string use the len() function:

len("NiceForm")  # Returns 8.

 

To convert any data type to string use the str() function:

f = 123.56
result.Value = str(f)

String Slices

A segment of a string is called a slice. Selecting a slice is similar to selecting a character:

s = "NiceLabel, NiceForm and NiceLabel Portal"

print s[0:9]     # NiceLabel
print s[11:19]   # NiceForm
print s[24:41]   # NiceLabel Portal

 

The operator [n:m] returns the part of the string from the "n-eth" character to "m-eth" character, including the first but excluding the last. If you omit the first index (before the colon), the slice starts at the beginning of the string. If you omit the second index, the slice goes to the end of the string.

s[:9]  # NiceLabel
s[24:] # NiceLabel Portal

 

Working with cases

There are several functions that allow you to work with character case.

s = "NiceLabel Portal"

# String converted to lower case.
lowercase = s.lower() # "nicelabel portal" 

# String converted to upper case.
uppercase = s.upper() # "NICELABEL PORTAL" 

# String converted to title case. 
titlecase = s.title() # "Nicelabel Portal"

# Make all lowercase letters uppercase and vice versa.
swapcase = s.swapcase() # "nICElABEL pORTAL" 

# The capitalize method is like title except that it considers the 
# entire string  to be a word. (i.e. it makes the first character 
# upper case and the rest lower case). 
capitalcase = s.capitalize() # "Nicelabel portal"

Searching for substrings

The find() method finds a substring within a larger string. It returns the leftmost index where the substring is found. If it is not found, –1 is returned. For searching in strings you can also use startswith(), endswith(), rfind(), count() and replace() methods. See attached files for more details.

 

Stripping

strip(), lstrip() and rstrip() methods can be used to remove characters you do not want in the string:

word = "999aaa999"
stripped = word.strip('9')    # returns "aaa"
lstripped = word.lstrip('9')  # returns "aaa999" 
rstripped = word.rstrip('9')  # returns "999aaa"

 

Checking Character Types

To check if a string contains a certain type of characters, use the following methods:

  • isalnum(): Returns true if string has at least 1 character and all characters are alphanumeric and false otherwise. 
  • isalpha(): Returns true if string has at least 1 character and all characters are alphabetic and false otherwise. 
  • isdigit(): Returns true if string contains only digits and false otherwise.
  • islower(): Returns true if string has at least 1 cased character and all cased characters are in lowercase and false otherwise. 
  • isspace(): Returns true if string contains only whitespace characters and false otherwise.
  • istitle(): Returns true if string is properly "title cased" and false otherwise.
  • isupper(): Returns true if string has at least one cased character and all cased characters are in uppercase and false otherwise.
  • isnumeric(): Returns true if string contains only numeric characters and false otherwise.

     

Padding strings

center() method returns a string that is centered in a string of a defined length. Padding is done using the specified fill character.  ljust() returns the string left justified in a string of specified length. Similarly goes for rjust().

word = "abcd"

# "   abcd   "
centered = '"' + word.center(10, ' ') + '"'

# "abcd      "
l = '"' + word.ljust(10, ' ') + '"'

# "      abcd"
r = '"' + word.rjust(10, ' ') + '"'

 

String Substitution

There are a few different ways to produce a string with information that is only available at run-time. The most obvious way is to concatenate multiple strings together using the + operator, but that only works if all the values are strings. As an alternative, Python also supports a way to inject objects into a string. This uses placeholders inside a string to denote where objects should go, along with a list of objects that should fill them in. This is called string substitution, and is performed using the % operator.

Placeholders consist of a percent sign and a conversion format. This allows the string to specify how objects should get converted, rather than having to call separate function explicitly. The most common of these formats is %s, which is equivalent to the str() function.

'This object is %s' % 1  # is equivalent to 'This object is 1'

 

Formatting

For a more powerful alternative to the simple string substitution described in the previous section, Python also includes a robust formatting system for strings. Rather than relying on a less obvious operator, string formatting uses an explicit format() method on strings. In addition, the syntax used for the formatting string is considerably different from what was used in simple substitution previously.

Instead of using a percent sign and a format code, format() expects its placeholders to be

surrounded by curly braces. What goes inside those braces depends on how you plan to pass in the values and how they should be formatted. The first portion of the placeholder determines whether it should look for a positional argument or a keyword argument. For positional arguments, the content is a number, indicating the index of the value to work with, while for keyword arguments, you supply the key that references the appropriate value.

s = "{0} and {1} are the best!"
s = s.format("NiceLabel", "NiceForm")

# NiceLabel and NiceForm are the best!

You can find all the samples here

Posted in NiceLabel, NiceLabel Portal Tagged with: , , ,