Python Numbers and strings

In this article we will have a look at numbers and strings because they are the base of development with Python.

Python Numbers

As you might already think, number data types store numeric values like integers and floating point numbers. Although there is one more types of numbers you should know about: complex numbers. Let’s take a brief look at these three types of numbers.

  • Integers (called ints too) are whole numbers without decimal points and can contain a positive or negative number. Integers can be as long as you wish providing good calculation possibilities[^longs].
  • Floats are floating point numbers, this means they contain a decimal point along with them even if they are whole numbers. They cover the set of real numbers so you can do any operation on them.
  • Complex numbers is for scientific purposes most of the time. But it is good to know there is something more. You probably know from maths that complex numbers are represented as a + bi where i is the imaginary part which represents the square root of -1. In Python however the imaginary part is represented as a j so if you want to have a complex number written you should do: c = 42+26j.

Special numbers

There are two commonly used (well, in mathematics at least) numbers which you can use in Python too. These number are pi and e. To use them, you have to import math module:

>>> import math
>>> math.pi
3.141592653589793
>>> math.e
2.718281828459045

Random numbers

When starting developing with the help of some books or tutorials you can encounter random number generation. We will use random numbers later. However it is a good time to introduce random number generation with Python.

For this we have to import a module: random.

>>> import random
>>> random.choice((1,2,3,4,'a')) # chooses one item of the provided list, tuple or string (it has to be an idexable object)
2
>>> random.random() # returns a random floating point number which is greater or equal to `0` and less than `1`
0.17489302862620337
>>> l = [1,2,3,4,5]
>>> >>> random.shuffle(l) # shuffles the list in place, returns `None`
>>> l
[1, 4, 3, 2, 5]
>>> random.randrange(1,10) # selects a random integer between the range of the first and the second parameter
4
>>> random.randrange(1,10,2) # the third parameter is an optional integer, defines the step in the range to select the elements from
5

Strings

Strings are in Python defined as a list of characters represented between quotation marks. We can use a single quote (‘ or apostrophe) or a double quote (“) to denote strings. We can use one within the other however if you start a string with a single quote you have to end it with it too.

These strings are single line strings, meaning you cannot spread them among multiple lines, you will get an error. However there is the string starting with three quotes (again single quote or double quote does not matter) we discussed when talking about comments. They can spread through multiple lines. However they are converted to regular strings at the end and have only the line-break character (\n) where you hit return when writing the text. The most common use of such a string is documentation. We will talk about this topic when we arrive at functions and classes.

Let’s have a quick example:

>>> "this is a
File "<stdin>", line 1
   "this is a
             ^
SyntaxError: EOL while scanning string literal

>>> 'this is line "
  File "<stdin>", line 1 # Starting and ending of String should be done with same type of quote
    'this is line "
                  ^
SyntaxError: EOL while scanning string literal

>>> 'this will " work " without any isue'
'this will " work " without any isue'
>>> ''' this is a
... string'''
' this is a\nstring'

Accessing and updating strings

Python does not have an explicit type of characters, they are treated as strings with a length of one. Accessing strings is the same than accessing any other variable.

Updating strings in-place is not an options because strings are immutable types in Python. If you “update” the value of a string you are only reassigning the value with a new string.

>>> s1 = "Hello World!"
>>> id(s1)
4318883504
>>> s1 = "Hello Python!"
>>> id(s1)
4318903344
>>> id('Hello World!')
4318883504

Special string operators

Because strings are types in Python which are treated as some kind of lists, strings have special operators which we can use for our development purposes. Let’s have a look at them with examples.

A detailed description on slicing follows in the next sub-section.

  • + sign concatenates the strings
  • * sign creates multiple copies of the string
  • [int] slices the character at the given position(int) out of the string
  • [int:int] It gets the characters between the given range(int – int)
  • [::int] gets the characters between the given range with the amount of steps(int) between the characters
  • in looks up if a substring is present in the string
  • not in looks if a substring is not present in the string
>>> 'Hello' + ' ' + "Python" # the + sign concatenates the strings
'Hello Python'
>>> 'Hello'+'!' * 5 # the * sign creates multiple copies of the string
'Hello!!!!!'
>>> "Python"[2] # slices the character at the given position out of the string
't'
>>> "Hello World!"[0:5] # gets the characters between the given range
'Hello'
>>> 'Hello World!'[::2] # gets the characters between the given range with the amount of steps between the characters
'HloWrd'
>>> 'llo' in "Hello World!" # looks up if a substring is present in the string
True
>>> "hell" not in 'Hello World!' # looks if a substring is not present in the string
True

Triple quotes

I’ve already mentioned my opinion about triple quotes: they are multiline strings and not comments.

Being a multiline string means that they print the same as single-line (or normal) strings but they preserve their explicit new lines and convert the special escape characters to their printed version.

>>> spam = """Spam, Spam, Spam, lovely Spam
... Wonderful Spam, Lovely Spam.
... Spam, Spam, Spam, magnificent Spam,
... \tSuperlative Spam.
... Spam, Spam, Spam, wonderous Spam,\nSurgical Spam, splendiferous Spam.
... Eggs, Bacon, sausage!\rSpam, Spam, Spam, Spaaam!"""
>>> spam
'Spam, Spam, Spam, lovely Spam\nWonderful Spam, Lovely Spam.\nSpam, Spam, Spam, magnificent Spam,\n\tSuperlative Spam.\nSpam, Spam, Spam, wonderous Spam,\nSurgical Spam, splendiferous Spam.\nEggs, Bacon, sausage!\rSpam, Spam, Spam, Spaaam!'
>>> print(spam)
Spam, Spam, Spam, lovely Spam
Wonderful Spam, Lovely Spam.
Spam, Spam, Spam, magnificent Spam,
   Superlative Spam.
Spam, Spam, Spam, wonderous Spam,
Surgical Spam, splendiferous Spam.
Spam, Spam, Spam, Spaaam!

As you can see, when not printing the string he special escape characters stay escaped — and even where we added a newline when creating the multiline-string Python explicitly displays the \ns.

String formatting

String formatting is cool in Python. There are two versions of formatting: with the % sign (which is similar to C’s printf method) and the format() method of the string itself. I will introduce both however in most of the cases the second version is better and easier to use.

Let’s jump right into the details.

>>> name = 'Bob'
>>> age = 31
>>> 'My name is %s and I am %d years young!'%(name, age)
'My name is Bob and I am 31 years young!'
>>> 'My name is {} and I am {} years young!'.format(name, age)
'My name is Bob and I am 31 years young!'
>>> 'My name is {0} and I am {1} years young!'.format(name, age)
'My name is Bob and I am 31 years young!'

As you can see, there is not much of a difference between the two formatting options. However if you look at the first version, you can see that I used different placeholders (%s and %d) for the variables than in the second version.

The key is that for the % formatting you have to tell the type of the parameters to enable python according format in the resulting string. This means too that you have to repeat variables if you want to print them more than once.

The format function works with the parameter-indexes and works out the formatting based on the type. So you do not have to explicitly mention what type the parameter is. And if you provide the same amount of parameters as placeholders you can leave the parameter indexes.

>>> 'My name is %s and I am %d years young and I have read %d books!'%(name, age, age)
'My name is Bob and I am 31 years young and I have read 31 books!'
>>> 'My name is {} and I am {} years young and I have read {} books!'.format(name, age)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
>>> 'My name is {} and I am {} years young and I have read {} books!'.format(name, age, age)
'My name is Bob and I am 31 years young and I have read 31 books!'
>>> 'My name is {0} and I am {1} years young and I have read {1} books!'.format(name, age)
'My name is Bob and I am 31 years young and I have read 31 books!'

It is up to you which formatting you use later on, however in this article series I will stick to the format function. However if you like the % better here are some characters and what they do in the format-string:

  • %c: character
  • %s: string conversion via str() prior to formatting
  • %i: signed decimal integer
  • %d: signed decimal integer
  • %u: unsigned decimal integer
  • %o: octal intege
  • %x: hexadecimal integer (lowercase letters)
  • %X: hexadecimal integer (uppercase letters)
  • %e: exponential notation (with lowercase ‘e’)
  • %E: exponential notation (with uppercase ‘E’)
  • %f: floating point real number
  • %g: the shorter of %f and %e
  • %G: the shorter of %f and %E
Series Navigation<< How to install python in windowsPython list >>
By | 2015-11-14T16:57:04+00:00 November 14th, 2015|Python Tutorial|0 Comments

About the Author:

Leave A Comment