DEV Community

Avnish
Avnish

Posted on

Python Print Variable – How to Print a String and Variable

Title : Python Print Variable – How to Print a String and Variable

I'll provide examples for each method you mentioned.

1. Using Concatenation:

variable = 10
print("The value of the variable is " + str(variable))
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • We define a variable variable and assign it the value 10.
  • We use the print() function to output a string concatenated with the value of the variable.
  • str(variable) is used to convert the integer variable into a string because you cannot concatenate a string with an integer directly.

Output:

The value of the variable is 10
Enter fullscreen mode Exit fullscreen mode

2. Using Comma Separation:

variable = 10
print("The value of the variable is", variable)
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Similar to the previous example, we define a variable variable and assign it the value 10.
  • In this case, we use the print() function with multiple arguments. The string and the variable are separated by commas.
  • Python automatically converts the variable into its string representation when printing.

Output:

The value of the variable is 10
Enter fullscreen mode Exit fullscreen mode

3. Using String Formatting:

variable = 10
print("The value of the variable is {}".format(variable))
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • We define a variable variable and assign it the value 10.
  • Inside the string, we use {} as a placeholder for the variable.
  • We then use the format() method to insert the value of the variable into the string.

Output:

The value of the variable is 10
Enter fullscreen mode Exit fullscreen mode

4. Using f-strings (Formatted String Literals):

variable = 10
print(f"The value of the variable is {variable}")
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Similar to the previous examples, we define a variable variable and assign it the value 10.
  • We use an f-string, denoted by the f before the string. Inside the string, we can directly place the variable within curly braces {}.
  • Python will replace these placeholders with the actual value of the variable when printing.

Output:

The value of the variable is 10
Enter fullscreen mode Exit fullscreen mode

5. Using Multiple Variables with f-strings:

variable1 = 5
variable2 = "hello"
print(f"The value of the first variable is {variable1} and the value of the second variable is '{variable2}'")
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • We define two variables variable1 and variable2 with values 5 and "hello", respectively.
  • In the f-string, we use multiple placeholders {} to represent each variable, and we directly place them within the string.
  • Python will replace these placeholders with the actual values of the variables when printing.

Output:

The value of the first variable is 5 and the value of the second variable is 'hello'
Enter fullscreen mode Exit fullscreen mode

Top comments (0)