Data Types and String Formatting

Introduction to data types and string formatting in Python.

In Python, there are four main data types:

  • Strings

  • Integers

  • Floats

  • Booleans

Strings

You've already used strings, but there's more to learn about them! In particular, string formatting.

What if we wanted to output a welcome message, but we didn't know the user's name yet? How about writing a sentence that welcomes the user, but where their name is supposed to go, we just put a placeholder? This is how string formatting works!

username = "cooldog123"

​message = "Hello {}, welcome to my cool Python program!".format(username)
print(message)

Here, we're using the format() function on the message string, and giving it the username variable to fill in the placeholder which is identified using curly braces '{}'.

The output for this program would be: "Hello cooldog123, welcome to my cool Python program!"

Integers

You may have wondered in the last section whether or not you're allowed to define a variable with the value of a number without quotes, for example:

age = 19

In fact, yes, you can! This data type is called an integer.

Here's a sample program that adds two integers:

numberOne = 5
numberTwo = 6
print(numberOne + numberTwo)

The output for this program would be 11.

Floats

How about adding numbers with decimal points? Yes, you can do that too, using a data type called a float!

Here's a sample program that adds two floats:

floatOne = 5.05
floatTwo = 6.001
print(floatOne + floatTwo)

The output for this program would be 11.051.

Booleans

Booleans have two values. True or False.

isCool = True
print(isCool)

The output for this program would be True.

Python is case-sensitive! This means you must ensure you always capitalise True or False whenever using them as booleans in your programs.

Last updated