Data types and type casting

In programming, data type is an important concept.

Variables can store data of different types, and different types can do different things.

Python has the following data types built-in by default, in these categories:

Text Type: str

Numeric Types: int, float, complex

Sequence Types: list, tuple, range

Mapping Type: dict

Set Types: set, frozenset

Boolean Type: bool

Binary Types: bytes, bytearray, memoryview

Strings

Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters.

However, Python does not have a character data type, a single character is simply a string with a length of 1.

Square brackets can be used to access elements of the string.

# Get the character at position 1 (remember that the first character has the position 0):
a = "Hello, World!"
print(a[1])

Output:

e

Slicing

You can return a range of characters by using the slice syntax.

Specify the start index and the end index, separated by a colon, to return a part of the string.

# Get the characters from position 2 to position 5 (not included):
b = "Hello, World!"
print(b[2:5])

Output:

llo

Negative Indexing

Use negative indexes to start the slice from the end of the string:

# Get the characters from position 5 to position 1, starting the count from the end of the string:
b = "Hello, World!"
print(b[-5:-2])

Output:

orl

String Length

To get the length of a string, use the len() function.

# The len() function returns the length of a string:
a = "Hello, World!"
print(len(a))

Output:

13

String Methods

Python has a set of built-in methods that you can use on strings.

# The strip() method removes any whitespace from the beginning or the end:
a = " Hello, World! "
print(a.strip()) # returns "Hello, World!"

Output:

Hello, World!
# The lower() method returns the string in lower case:
a = "Hello, World!"
print(a.lower())

Output:

hello, world!
# The upper() method returns the string in upper case:
a = "Hello, World!"
print(a.upper())

Output:

HELLO, WORLD!
# The replace() method replaces a string with another string:
a = "Hello, World!"
print(a.replace("H", "J"))

Output:

Jello, World!
# The split() method splits the string into substrings if it finds instances of the separator:
a = "Hello, World!"
print(a.split(","))

Output:

['Hello', ' World!']

Python Casting

Specify a Variable Type

There may be times when you want to specify a type on to a variable. This can be done with casting. Python is an object-orientated language, and as such it uses classes to define data types, including its primitive types.

Casting in python is therefore done using constructor functions:

  • int() - constructs an integer number from an integer literal, a float literal (by rounding down to the previous whole number), or a string literal (providing the string represents a whole number)
  • float() - constructs a float number from an integer literal, a float literal or a string literal (providing the string represents a float or an integer)
  • str() - constructs a string from a wide variety of data types, including strings, integer literals and float literals
x = int(1)   # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3