Strings

The type string is a type used for text:

name = 'Johan Carlsson'
child = "Åsa is Johan Carlsson's daughter"
book = """Aunt Julia 
       and the Scriptwriter"""

A string is enclosed either by single or double quotes. If a string contains several lines, it has to be enclosed by three double quotes """ or three single quotes '''.

Strings can be indexed with simple indexes or slices (refer to Chapter 3, Container Types, for a comprehensive explanation on slices):

book[-1] # returns 'r' 
book[-12:] # returns 'Scriptwriter'

Strings are immutable; that is, items cannot be altered. They share this property with tuples. The command book[1] = 'a' returns:

TypeError: 'str' object does not support item assignment

The string ' ' is used to insert a line break  and 't' inserts a horizontal tabulator (TAB) into the string to align several lines:

print('Temperature:	20	C
Pressure:	5	Pa')

These strings are examples of escape sequences. Escape sequences always start with a backslash, . A multi line string automatically includes escape sequences:

a=""" 
A multiline 
example""" 
a #  returns '
A multiline 
example'

A special escape sequence is "", which represents the backslash symbol in text:

latexfontsize="\tiny"

The same can be achieved by using a raw string instead:

latexfs=r"	iny"   # returns "	iny"
latexfontsize == latexfs  # returns True

Note that in raw strings, the backslash remains in the string and is used to escape some special characters:

r"""   # returns  '\"'
r"\"   # returns  '\\'
r""    # returns an error

Operations on strings and string methods

Addition of strings means concatenation:

last_name = 'Carlsson'
first_name = 'Johanna'
full_name = first_name + ' ' + last_name
                              # returns 'Johanna Carlsson'

Multiplication is just repeated addition:

game = 2 * 'Yo' # returns 'YoYo'

When strings are compared, lexicographical order applies and the uppercase form precedes the lowercase form of the same letter:

'Anna' > 'Arvi' # returns false 
'ANNA' < 'anna'  # returns true 
'10B' < '11A'    # returns true

Among the variety of string methods, we will mention here only the most important ones:

  • Splitting a string: This method generates a list from a string by using a single or multiple blanks as separators. Alternatively, an argument can be given by specifying a particular string as a separator:
          text = 'quod erat    demonstrandum'
          text.split() # returns ['quod', 'erat', 'demonstrandum']
          table = 'Johan;Carlsson;19890327'
          table.split(';') # returns ['Johan','Carlsson','19890327']
          king = 'CarlXVIGustaf'
          king.split('XVI')  # returns ['Carl','Gustaf']
    
    
  • Joining a list to a string: This is the reverse operation of splitting:
          sep = ';'
          sep.join(['Johan','Carlsson','19890327']) 
          # returns 'Johan;Carlsson;19890327'
    
  • Searching in a string: This method returns the first index in the string, where a given search substring starts:
          birthday = '20101210'
          birthday.find('10') # returns 2 

    If the search string is not found, the return value of the method is -1 .

String formatting

String formatting is done using the format method:

course_code = "NUMA21"
print("This course's name is {}".format(course_code)) 
# This course's name is NUMA21

The function format is a string method; it scans the string for the occurrence of placeholders, which are enclosed by curly brackets. These placeholders are replaced in a way specified by the argument of the format method. How they are replaced depends on the format specification defined in each {} pair. Format specifications are indicated by a colon, ":", as their prefix.

The format method offers a range of possibilities to customize the formatting of objects depending on their types. Of particular use in scientific computing are the formatting specifiers for the float type. One may choose either the standard with {:f} or the exponential notation with {:e}:

quantity = 33.45
print("{:f}".format(quantity)) # 33.450000
print("{:1.1f}".format(quantity)) # 33.5
print("{:.2e}".format(quantity)) # 3.35e+01

The format specifiers allow to specify the rounding precision (digits following the decimal point in the representation). Also the total number of symbols including leading blanks to represent the number can be set.

In this example, the name of the object that gets its value inserted is given as an argument to the format method. The first {} pair is replaced by the first argument and the following pairs by the subsequent arguments. Alternatively, it may also be convenient to use the key-value syntax:

print("{name} {value:.1f}".format(name="quantity",value=quantity))
# prints "quantity 33.5"

Here, two values are processed, a string name without a format specifier and a float value that is printed in fixed point notation with one digit after the decimal point. (We refer to the complete reference documentation for more details on string formatting, [34]).

Tip

Braces in the string

Sometimes, a string might contain a pair of curly braces, which should not be considered as placeholders for a format method. In that case, double braces are used:

r"we {} in LaTeX egin{{equation}}".format('like')

This returns the following string: 'we like in LaTeX \begin{equation}'.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.131.255