Information

Section: Strings
Goal: Understand the particularities of the type String in Python.
Time needed: 15 min
Prerequisites: Curiosity

Strings

Credits: Institute of Maritime Logistics – Hamburg University of Technology

1 The Print Statement

As seen previously, The print() function prints all of its arguments as strings (another word for text), separated by spaces and followed by a linebreak. If the arguments are separated by commas, the output is separated by a space, otherwise the strings follow one another directly.

print("Hello World")
print("Hello", "World")
print("Hello Wo" "rld")
Hello World
Hello World
Hello World

Variables or a combination of text and variables can also be entered into the print() function:

w = 'World'
print("Hello", w)
Hello World

Note that print is different in old versions of Python (2.7) where it was a statement and did not need parenthesis around its arguments.

The print has some optional arguments to control where and how to print. This includes sep the separator (default space), which argues what should occupy spaced. Then end argument which adds characters to the end of the string. And file to specify where to write to the print function.

Optional arguments are parameters within a command with preexisiting defaults that can be changed to customize the command. These ‘parameters’ can slightly alter a function’s output. In this case it changes the separator and end of a print function.

print("Hello","World",sep='...',end='!!')
Hello...World!!

2 String Formating

There are lots of methods for formating and manipulating strings built into python. Some of these are illustrated here.

String concatenation is the “addition” of two strings. Observe that while concatenating there will be no space between the strings.

string1='World'
string2='!'
print('Hello' + string1 + string2)
HelloWorld!

The ‘print’ by default returns everything as a string. The % operator is used to format a string inserting the value that comes after. The most common types of format specifiers are:

- %s -> string
- %d -> Integer
- %f -> Float

This can be handy when writing something repetitive or needing numbers to be encoded as integers or floats.

For this purpose, ‘%s’ or ‘%d’ or ‘%f’ is written within the “normal input”, this acts as a placeholder. After the “normal input” there is then a ‘%’ followed by the respective value(s) to be inserted at the place of the placeholder (and spaces are ignored if they are not part of the string).

print("Hello %s" % string1)           # The '%s' denotes the format to change to, the % denotes what value to change.
print("Actual Number = %d" %18)
print("Float of the number = %f" %18)
print("Float of the number = %f" %18)
Hello World
Actual Number = 18
Float of the number = 18.000000
Float of the number = 18.000000

When referring to multiple variables parenthesis are used. Values are inserted in the order they appear in the parenthesis.

print("Hello %s %s. This meaning of life is %d" %(string1,string2,42))
Hello World !. This meaning of life is 42

We can also specify the width of the field and the number of decimal places to be used. For example:

print('Print width 10: |%10s|'%'xy') 
# The '%' starts the formatting, '10' signals the number of charters spaces, 's' identifies it as a string value,
# since xy is only 2 characters, the rest (from the left) is filled with spaces.
# This is called "padding".
print("The number pi = %.2f to 2 decimal places"%3.1415) 
# The '.2' commands two decimal places and the 'f' commands a float value.
print("More space pi = %10.2f"%3.1415) # An example of padding plus decimal formatting.
print("Pad pi with 0 = %010.2f"%3.1415) # The 0 before the 10 indicates that the space is to be filled with zeros.
Print width 10: |        xy|
The number pi = 3.14 to 2 decimal places
More space pi =       3.14
Pad pi with 0 = 0000003.14

3 Other String Methods

Multiplying a string by an integer simply repeats it:

print("Hello World! "*5)
Hello World! Hello World! Hello World! Hello World! Hello World! 

Strings can be tranformed by a variety of functions:

hw = "hello woRLD"
print(hw.capitalize()) # capitalizes first letter
print(hw.title()) # capitalizes entire string
print(hw.lower()) # miniscules entire string
print('|%s|' % hw.center(30)) # The string is printed in the middle of 30 spaces.
print('|%s|' % "     lots of space             ".strip()) # This deletes blanks at the beginning and end of the string.
print(hw.replace("woRLD", "Class")) # Searches and replaces specific values
Hello world
Hello World
hello world
|         hello woRLD          |
|lots of space|
hello Class

There are also lots of ways to inspect or check strings. Examples of a few of these are given here:

hw="Hello World"
print("The length of '%s' is"%hw,len(hw),"characters") # len() gives length
print(hw.startswith("Hello") and hw.endswith("World")) # check start/end
# count strings:
print("There are %d 'l's but only %d World in %s" % (hw.count('l'),hw.count('World'),hw)) 
# '.count()' can be told to count specific values
print('"el" is at index',hw.find('el'),"in",hw)
The length of 'Hello World' is 11 characters
True
There are 3 'l's but only 1 World in Hello World
"el" is at index 1 in Hello World

4 Accessing parts of strings

Strings can be indexed with square brackets. Indexing starts from zero in Python.

ch = 'abcdefghi'
print('First charcter of',ch,'is',ch[0]) 
# last term calls for the value of first character in variable 's' (counting starts at 0)
print('Third charcter of',ch,'is',ch[2])
print('Last charcter of',ch,'is',ch[len(ch)-1]) 
# the last term calls for the last value of the variable, at the position 
# 8 = 9-1 = len(ch)-1 = "Number of characters of ch" - 1 
#(since counting starts with 0, the -1 is needed to get the last value)
First charcter of abcdefghi is a
Third charcter of abcdefghi is c
Last charcter of abcdefghi is i

Negative indices can be used to start counting from the back

print('First charcter of',ch,'is',ch[-len(ch)]) 
# Last term is read as -9, the - signals the code to count the string from right to left
print('Last character of',ch,'is',ch[-1])
First charcter of abcdefghi is a
Last character of abcdefghi is i

Finally a substring (range of characters) can be specified as using \(a:b\) to specify the characters at index \(a,a+1,\ldots,b-1\). Note that the last character is not included. This ensures that the number of characters of [x:y] is y-x.

print("First three characters",ch[0:3])
print("Next three characters",ch[3:6])
First three characters abc
Next three characters def

An empty beginning/end of the range denotes the first/last character of the string:

print("First three characters", ch[:3])
print("Last three characters", ch[-3:])
First three characters abc
Last three characters ghi

5 Strings are immutable

It is important that strings are constant, immutable values in Python. While new strings can easily be created it is not possible to modify a string:

ch = 'abcdefghi'
chX = ch[:2]+'X'+ch[3:] # this creates a new string (with the name chX), with c replaced by X
print("creating a new string:", chX)
chX2 = ch.replace('c','X') # this also creates a new string (with the name chX2), with c replaced by X
print("creating a second new string:", chX2)
print("the original string stays the same:", ch)
creating a new string: abXdefghi
creating a second new string: abXdefghi
the original string stays the same: abcdefghi

Uncomment the following line by removing the #:

#ch[2] = 'X' # a string can not be changed in this way! -> Error

6 Type Function

The type function can call upon a an argument and returns the calls type of the object:

a = 1
print("a   is of type", type(a))
print("1   is of type", type(1))

b = 1.0
print("b   is of type", type(b))
print("1.0 is of type", type(1.0))

c = '1'
print("c   is of type", type(c))
print("'1' is of type", type('1'))
a   is of type <class 'int'>
1   is of type <class 'int'>
b   is of type <class 'float'>
1.0 is of type <class 'float'>
c   is of type <class 'str'>
'1' is of type <class 'str'>