Strings, the good, the bad, and the ugly

String indexing

The character components of a string can be accessed the same way that a list can be. Each character as a positional assignment, so for example:

-11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
h e l l o   w o r l d
0 1 2 3 4 5 6 7 8 9 10

You can index and slice a string just like you would a list, so for example:

In [1]: my_string = 'hello world'

In [2]: my_string[0]
Out[2]: 'h'

In [3]: my_string[-1]
Out[3]: 'd'

In [4]: my_string[3:9]
Out[4]: 'lo wor'

String operators & functions

Strings have access to two operators + and *, and the behaviour is just like lists, so:

In [5]: my_string = 'spam'

In [6]: my_string + 'eggs'
Out[6]: 'spameggs'

In [7]: my_string * 5
Out[7]: 'spamspamspamspamspam'

There are a number of useful functions associated with string objects. Python strings are case sensitive so:

In [8]: 'spam' == 'SPAM'
Out[8]: False

In [9]: 'spam' != 'SPAM'
Out[9]: True

Luckily there are a number of functions to assist with various capitalisation choices that you may want:

In [10]: my_string = 'your mother was a haMster and your father smElt of elderberries'

In [11]: my_string.lower() # all characters to lower case
Out[11]: 'your mother was a hamster and your father smelt of elderberries'

In [12]: my_string.upper() # all characters to upper case
Out[12]: 'YOUR MOTHER WAS A HAMSTER AND YOUR FATHER SMELT OF ELDERBERRIES'

In [13]: my_string.capitalize() # first character to upper
Out[13]: 'Your mother was a hamster and your father smelt of elderberries'

In [14]: my_string.title() # the first character of each word capitalized
Out[14]: 'Your Mother Was A Hamster And Your Father Smelt Of Elderberries'

There are also functions to help break up and put together strings.

In [15]: print(my_string)
your mother was a haMster and your father smElt of elderberries

# .split() splits the sting into a list of stings by removing the characters specified (a space in this case)
In [16]: temp = my_string.split(' ')

In [17]: type(temp)
Out[17]: list

In [18]: print(temp)
['your', 'mother', 'was', 'a', 'haMster', 'and', 'your', 'father', 'smElt', 'of', 'elderberries']

# ''.join() takes as list of strings and joins them together with the characters in proceeding string
In [19]: '-'.join(temp)
Out[19]: 'your-mother-was-a-haMster-and-your-father-smElt-of-elderberries'

Finally there are some useful functions to remove characters from strings:

# ''.strip() removes characters from the beginning and/or end of a sting, the default is a space
In [20]: test = '*********lunch*lunch***********'

In [21]: test.strip('*')
Out[21]: 'lunch*lunch'

# ''.replace(old, new) finds all instances of the old string and replaces it with the new string
In [22]: test.replace('lunch', 'dinner')
Out[22]: '*********dinner*dinner***********'

Formatted output

In python there is an elegant way to create formatted string outputs using the ‘’.format() method. The basic premise of the format method is that you pass the arguments in the function into specific places into the proceeding string. So for example:

# passing arguments('spam' and 'eggs') by position into the {}
In [23]: print("I don't like {} or {}".format('spam', 'eggs'))
I don't like spam or eggs

# passing arguments by keywords into the {}
In [24]: print("""{p} ran away. {adv} ran away away.
   ....: When danger reared it’s ugly head, he {adv} turned his tail and fled.
   ....: {p} turned about and gallantly he chickened out""".format(p='Brave Sir Robin', adv='Bravely'))
   ....: 
Brave Sir Robin ran away. Bravely ran away away.
When danger reared it’s ugly head, he Bravely turned his tail and fled.
Brave Sir Robin turned about and gallantly he chickened out

# you can pass anything into the .format method that can be passed to the print function
In [25]: my_list = [1,2,3]

In [26]: my_dict = {'Bert': 'Ernie'}

In [27]: my_number = 42

In [28]: my_new_string = 'like lists : {}, dictionaries: {}, and numbers: {}.'.format(my_list, my_dict, my_number)

In [29]: print(my_new_string)
like lists : [1, 2, 3], dictionaries: {'Bert': 'Ernie'}, and numbers: 42.

Passing values back into a string is useful enough, but the .format() method of strings gives significantly more control over how the object is transformed into a sting. Below we’ll cover the most commonly used formatting options for environmental scientists, but For a deeper dive into the full capabilities of python formatted output, check out this lovely tutorial.

In [30]: '{:4d}'.format(42)  # at least 4 digits, padded with spaces
Out[30]: '  42'

In [31]: '{:04d}'.format(42)  # at least 4 digits, padded with zeros
Out[31]: '0042'

In [32]: '{:4d}'.format(42666666) # note that this can go beyond 4 digits
Out[32]: '42666666'

In [33]: '{:6.2f}'.format(3.14159265)  # at least 6 digits, padded with spaces, with exactly 2 digits after the decimal point
Out[33]: '  3.14'

In [34]: '{:06.2f}'.format(3.14159265)  # as above but padded with zeros
Out[34]: '003.14'