How to Print a String and Variable in Python [6 Ways]

Are you looking for different ways to print variables and strings on the same line in Python? In this Python article, I will tell you six different ways to print a string and variable in Python with some illustrative examples.

To print a string and variable in Python, we can use the basic print() function, concatenate them with the + operator, separate them with commas inside print(), or format them using .format(), f-strings, or the % operator.

Print a string and variable in Python

There are six different methods to print a string and variable in Python:

  1. The print() Function
  2. Using Concatenation
  3. Using a Comma in print()
  4. Using String Formatting with .format()
  5. Using F-Strings
  6. Using % for String Formatting

Let’s see them one by one using some illustrative examples:

1. Python print string and variable using the print() function

This method involves using Python’s built-in print() function to display strings or variable values directly to the console. It’s the most straightforward and commonly used method for output in Python.

Here is the code to print a string and variable in Python using the print() function:

Hello_Message = "Welcome to New York City!"
print(Hello_Message)

Output:

Welcome to New York City!

Upon completion of code execution in Pycharm, the resulting output is displayed in the screenshot below.

How to Print a String and Variable in Python

2. Print variable and string on same line in Python using concatenation

Concatenation involves combining strings or variables into a single string using the + operator. This method is useful for creating a customized output string from multiple string or variable components. We can concatenate as many variables as we want using the + operator.

Here is the use of the concatenation operation to print a string and variable in Python:

city = "Los Angeles"
state = "California"
print(city + ", " + state)

Output:

Los Angeles, California

A screenshot is provided below, capturing the results post-execution of the code in the Pycharm editor.

How to print variables in Python

3. Python to print variable in strings using a comma in print() function

When using a comma in the print() function, Python automatically inserts a space between the items. We can separate many variable names with commas and the print() function will print all of them in one time.

Let’s try to print a string and variable in Python using commas in print() function:

sport1 = "Baseball"
sport2 = "Basketball"
print("Popular American sports include", sport1, "and", sport2 + ".")

Output:

Popular American sports include Baseball and Basketball.

Following the execution of the code in the Pycharm editor, a screenshot of the output is presented below.

How to Print a Variable and a string on the same line in Python

4. How to print a variable in Python using string formatting with .format() function

The .format() method is a versatile way to insert variables into a string. Placeholders {} in the string are replaced with the values (variable) provided in the .format() method in Python, offering a readable and flexible way to format strings.

Let’s see an instance of how to print a string and variable in Python using string formatting with .format() function:

landmark = "Mount Rushmore"
state = "South Dakota"
print("The {} is located in {}.".format(landmark, state))

Output:

The Mount Rushmore is located in South Dakota.

Following the execution of the code within the Pycharm editor, the resulting screenshot is presented below.

How to Print a String and a Variable in Python

5. Print a string and a variable in Python using f-strings

F-strings are introduced in Python 3.6, enabling embedding expressions directly inside string literals using {} syntax.

Here is the code:

president = "George Washington"
year = 1789
print(f"{president} became the first President of the United States in {year}.")

Output:

George Washington became the first President of the United States in 1789.

Below is a screenshot showcasing the results post-implementation of the code in the Pycharm editor.

print variable and a string in python

6. Print string and variable in Python using % for string formatting

This older method uses the % operator for string formatting. It replaces % specifiers in a string with variable values. For each datatype, we need to clarify the type like %s is for string, %d for integer, %f for float, etc.

Here is the code:

company = "Apple Inc."
founded = 1976
print("The company %s was founded in %d." % (company, founded))

Output:

The company Apple Inc. was founded in 1976.

Post the code execution in Pycharm, the output is captured in the screenshot provided below.

Printing variables and strings in Python

Conclusion

Here, I have explained six diverse methods to print a string and variable in Python: using the basic print() function, concatenation, inserting commas, applying string formatting with .format(), leveraging F-strings, and utilizing the % operator for string formatting.

Each technique offers unique advantages, catering to various scenarios and preferences in Python programming.

You may also like to read: