How to Define and Print Strings as Variables In Python 3

Printing and defining strings as variables in Python is essential, and in almost any Python program, you will probably come across this need.

In this post, we will define a string variable that holds a string and print it with extra text around it. For more Python posts, please visit the Python category page.

Format String

In Python, we define a string with double quotes (” “) or a single quote (‘ ‘) . By doing that, Python knows we are working with a string.

Let’s go ahead and defined a string using the following line.

blog_url = ''

Print a String Variable

Now that we have our string variable defined and set, let’s go ahead and print it out.

To print a string variable, we need to follow the following rules.

  1. We need to put the latter f before we print
  2. We need to put the variable inside {}

Let’s get into action and print our string using the following code.

blog_url = ''
print(f"The blog URL is: {blog_url}")

The outcome of the code is:

The blog URL is:

Leave a comment

Your email address will not be published.