Python Data Types

Strings

Strings are sequences of text characters. Text analytics is a domain where Python has proven to be quite powerful, so strings is one of the most important native data types.

You can create a Python string by enclosing characters in either single quotes or double
quotes. However be mindful of how your code is going to be run, as it can be the case that one or the other is more preferable. For example, some server architectures(like XAMPP) don’t play nice with single quotes. You can assign strings to variables like so.

my_string = 'Hello'
my_string = "Hello"

The len() function also works here and can give you the length of a stirng.

my_string = 'Hello'
len_num = len(my_string)

There are also triple quotes, three single quotes (”’) or three double quotes (“””).

my_string = '''Hello'''
my_string = """Hello"""

Their most common use is to create multiline strings, like if you wanted to write a rap song in Python.

my_verse = """I'm Slim Shady, yes, I'm the real Shady
All you other Slim Shadys, are just imitating
So won't the real Slim Shady, please stand up?
Please stand up, please stand up"""

You can convert other data types to strings using the native Python function str().

my_string = str(1234)

You can concatenate strings(meaning combine them to a single string) with the + sign.

my_sting1 = "I'm Slim Shady, "
my_string2 = " yes, "
my_string3 = "I'm the real Shady"
total_string = my_sting1 + my_string2 + my_string3

You can access characters in a string by indexing just like in lists using the syntax [ start : end : step ].

my_lyric = "So won't the real Slim Shady, please stand up?"
my_lyric[0] #Access the 1st character
my_lyric[-1] #Access the last character
my_lyric[0:2] #Access the 1st word
my_lyric[0:10:2] #Characters from position 0 to 10, with a step 3

You can split a string on a specific character using the .split() method. Keep in mind this method will return a list of strings.

my_lyric = "I am G, I'm the realest, I'm the OG"
list_of_strings = my_lyric.split(",")

If you want to join a list of strings into a single string, so the inverse of what we did above you can use the .join() method with a specific unifying character.

list_of_lyrics = ["Please stand up","please stand up", "please stand up"]

my_lyric = ",".join(list_of_lyrics)

Another handy method is the .replace(). This method will substitute an existing substring of your string with another.

my_lyric =  "Please stand up, please stand up, please stand up"
new_lyric = my_lyric.replace("up", "down")

The above methods are basic and general enough. They will give you an idea about string handling in Python. There are many other native functions and a wealth of external libraries for string handling. Generally it is not useful to just read about them. Without needing them for a specific reason, you will very likely forget about them almost instantly.

Integers and Floats

Integers are whole numbers such as 1, 2, 1234, 3400.

Floats are numbers with decimal points like 2.14, 3.14159, 9.999.

All simple math operations can be done with these numbers. What is more important is the int() and float() native functions that can convert other data types to ints or floats.

my_int = int('12') #converting a string to an int
my_float = float('3.14') #converting a string to a float

Booleans

Booleans are binary types and take only 2 values: True or False. Python will be able to do 1st order Logic operations with these types of variables using operators like and, or etc.

boolean1 = True
boolean2 = False
eval_and = boolean1 and boolean2 # Should evaluate to False
eval_or = boolean1 or boolean2 # Should evaluate to True