Learn How to Display Text and Numbers in Your Python Programs

This tutorial will guide you through the fundamentals of printing strings (text) and integers (numbers) in Python, a crucial skill for creating interactive and informative programs. …

Updated August 26, 2023



This tutorial will guide you through the fundamentals of printing strings (text) and integers (numbers) in Python, a crucial skill for creating interactive and informative programs.

Welcome to the world of Python! One of the first things you’ll learn is how to display information to the user. This is where the print() function comes in handy. Think of it as your program’s voice – it allows you to communicate results, messages, and data to anyone interacting with your code.

Understanding Strings and Integers

Before we dive into printing, let’s quickly recap what strings and integers are:

  • Strings: These are sequences of characters enclosed in single (’ ‘) or double (" “) quotes. They represent text, like names, sentences, or even code itself!
    • Example: name = "Alice"
  • Integers: These are whole numbers without any decimal points. They represent quantities, counts, or numerical values.
    • Example: age = 30

Printing Strings with print()

The print() function is incredibly straightforward to use. Simply place the string you want to display within the parentheses:

print("Hello, world!")

This code will output:

Hello, world!

Let’s break it down:

  • print(): This is the Python function responsible for displaying output.
  • "Hello, world!": This is our string literal – the text we want to print. Notice it’s enclosed in double quotes.

Printing Integers with print()

Printing integers works just like printing strings:

age = 25
print(age)

This code will output:

25

Here, we first assign the integer value 25 to a variable named age. Then, we use print(age) to display its value.

Combining Strings and Integers

Often, you’ll want to print both strings and integers together in a meaningful way. To achieve this, we can use string formatting:

  • Using the + operator:
name = "Bob"
age = 30
print("My name is " + name + " and I am " + str(age) + " years old.")

This code will output:

My name is Bob and I am 30 years old.

Explanation:

  • We concatenate (join together) strings using the + operator.
  • Crucially, we use str(age) to convert the integer age into a string before adding it to other strings. Python won’t directly allow mixing different data types like this.

Important Note: Always remember to convert integers to strings (str()) before combining them with other strings using the + operator.

  • Using f-strings (formatted string literals): This is a more modern and efficient way to combine strings and variables:
name = "Alice"
age = 28

print(f"My name is {name} and I am {age} years old.")

Output:

My name is Alice and I am 28 years old.

Explanation:

  • The f before the opening quote indicates an f-string.
  • Inside the string, we enclose variable names within curly braces {}. Python will automatically replace these with the values of the corresponding variables.

Common Mistakes & Tips:

  1. Forgetting to Convert Integers: A frequent error is trying to directly combine strings and integers using +. Remember to use str(integer) to convert integers into strings before concatenating.
  2. Incorrect Quotes: Pay attention to whether you’re using single (') or double (") quotes for your strings. Python will throw an error if the opening and closing quotes don’t match.

Tips for Writing Readable Code:

  • Use meaningful variable names (e.g., user_name instead of un).
  • Add comments to explain complex parts of your code. Comments are ignored by Python but help humans understand what’s going on.

Let me know if you have any questions!


Stay up to date on the latest in Computer Vision and AI

Intuit Mailchimp