How to Print Strings and Variables in Python?

In this tutorial, I will explain how to print strings and variables in Python. As a data scientist working with large datasets of US demographics, I often need to output important information like names, addresses, and other variables in a readable format, then I researched more about this topic and I will share my findings with examples.

Print Strings and Variables in Python

Let us learn how to print strings and variables in Python.

Read How to Check if a String is Empty or NaN in Python?

Print Strings in Python

To print a simple string in Python, you can use the built-in print() function in Python. Simply enclose the string you want to print within quotes inside the parentheses. For example:

print("Hello, John from New York!")

Output:

Hello, John from New York!

I executed the above example code and added the screenshot below.

Print Strings and Variables in Python

You can use either single quotes ('...') or double quotes ("...") to define a string in Python. If your string contains a single quote character, you can surround it with double quotes to avoid syntax errors, and vice versa:

print('Sarah said, "I love living in California!"')

Output:

Sarah said, "I love living in California!"

Check out How to Check if a String is an Integer or Float in Python?

Print Multiline Strings

Sometimes you may need to print a string that spans multiple lines. In Python, you can use triple quotes ('''...''' or """...""") to create a multiline string:

print("""First line
Second line
Third line""")

Output:

First line
Second line
Third line

I executed the above example code and added the screenshot below.

How to Print Strings and Variables in Python

In Python, triple quotes (''' or """) allow the creation of multiline strings, preserving line breaks and formatting as written. This feature is particularly useful for handling large blocks of text, such as documentation or messages, without the need for explicit newline characters.

Read How to Check if a String Starts with a Specific Substring in Python?

Print Variables in Python

In addition to printing static strings in Python, you often need to print the values of variables. There are a few ways to accomplish this in Python.

1. Concatenate Strings and Variables

One approach is to concatenate strings and variables using the + operator in Python:

name = "Michael"
age = 35
print("My name is " + name + " and I am " + str(age) + " years old.")

Output:

My name is Michael and I am 35 years old.

I executed the above example code and added the screenshot below.

Print Strings and Variables in Python by concatenating

Note that you need to convert non-string variables (like integers) to strings using the str() function before concatenating.

Check out How to Check if a String Ends with a Pattern in Python?

2. Use f-Strings (String Interpolation)

In Python, f-strings (formatted string literals) provide a concise and efficient way to embed expressions within string literals. Introduced in Python 3.6, f-strings are created by prefixing a string with the letter ‘f’ or ‘F’. Expressions to be evaluated are placed inside curly braces {} within the string.

state = "Texas"
population = 29145505
print(f"{state} has a population of {population:,}")

Output:

Texas has a population of 29,145,505

In this example, the variables state and population are directly embedded into the string. The :, format specifier adds commas to the population number for better readability. F-strings support various format specifiers, allowing for control over number formatting, alignment, and more.

Read How to Check if a String is All Uppercase in Python?

3. Use the format() Method

The format() method in Python allows for dynamic insertion of variables into strings using placeholders defined by curly braces {}. In the provided example, city and founded are inserted into the string, resulting in:

city = "Chicago"
founded = 1837
print("Welcome to {}! Founded in {}.".format(city, founded))

Output:

Welcome to Chicago! Founded in 1837.

This method enhances code readability and maintainability by clearly separating the static and dynamic parts of the string. It’s particularly useful when constructing strings that require variable data, such as user messages or formatted reports.

Read How to Check if a String is ASCII in Python?

Conclusion

In this article, I helped you to learn how to print strings and variables in Python. I explained printing string and printed the multiline string. Printing variables by concatenating string and variables, using f-string and variables, and using the format() methods.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.