Hey! If you love Python and building Python apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Printing Strings and Integers in Python

In this tutorial, we will cover the fundamentals of printing strings and integers in Python. We’ll define what printing means in the context of programming, explain how it relates to strings and integ …


Updated June 23, 2023

In this tutorial, we will cover the fundamentals of printing strings and integers in Python. We’ll define what printing means in the context of programming, explain how it relates to strings and integers in Python, provide step-by-step instructions on how to print these data types, and offer practical examples to solidify your understanding.

What is Printing in Programming?

In computer science, printing refers to the process of displaying output on the screen or sending it to a file. In programming languages like Python, printing allows you to show the results of calculations, values of variables, or messages that are executed during the program’s execution.

Strings and Integers in Python

Python supports two basic data types: strings (sequences of characters) and integers (whole numbers). You can store these values in variables for later use. To display them on the screen, you’ll need to use a print function.

Printing Strings

To print strings in Python, you’ll use the print() function followed by the string value enclosed in quotes. Here’s an example:

# This is how you print a simple message or string
greeting = "Hello, World!"
print(greeting)

Explanation:

  • print(): This is the built-in function used to output values.
  • "Hello, World!": This is the string value that will be printed on the screen. The quotes (") indicate it’s a string.

When you run this code, Python will display the message “Hello, World!” on the screen.

Printing Integers

Similarly, to print integers in Python, simply use the print() function followed by the integer value without any quotes:

# This is how you print an integer
age = 25
print(age)

Explanation:

  • print(): Again, this is the function used for output.
  • 25: This is the integer value to be printed. It’s just a number.

When you run this code, Python will display the number “25” on the screen.

Combining Strings and Integers

You can also mix strings and integers in your print statements:

# Mixing string and integer values in one print statement
print("My age is", 25)

In this example, print() outputs a message that includes both a string (“My age is”) and an integer value (25).

Step-by-Step Recap

To summarize:

  1. Define Printing: Understand what printing means in programming.
  2. Know Your Data Types: Be familiar with strings and integers as basic data types in Python.
  3. Use print() for Strings: Print string values using quotes around the text.
  4. Print Integers Directly: Use the print() function to display integer values without quotes.
  5. Combine Text and Numbers: To mix both, use the print() function with your message followed by the number without quotes.

By following these steps and understanding how strings and integers work in Python, you’re well-equipped to print various data types with ease. Happy coding!

Stay up to date on the latest in Python, AI, and Data Science

Intuit Mailchimp