Sometimes variables holding different types of data need to be integrated into a single variable. The integration/combination of various data types into a single variable is referred to as data concatenation. Python provides several functionalities that can print the string with variables. This Python blog will shed light on different methods to print strings and variables of different data types on the same line.
Printing Variables and Strings on the Same Line in Python
The print statement in Python can print/display the variables’s values of any type. Python provides different functions that can print the variables and strings on the same line in Python. For practical demonstration, consider the following methods:
- Method 1: Using Concatenation
- Method 2: Using Commas
- Method 3: Using f string
- Method 4: Using .format()
- Method 5: Using print + str()
- Method 6: Using % Operator
Let us discuss each method in detail.
Method 1: Using Concatenation
Integrating data is referred to as concatenation. Concatenation can be done using the + operator. The variables and strings can be written/printed together in a similar line using the concatenation method. This method works only if the variable contains string data. This approach will not work in any other scenario. The following code explains how a variable and a string can be written on the same line using concatenation in Python:
var= "20 travel"
print('I have ' + var +' stamps.' )
In the above code,
- A variable var is defined, having the string data “20 travel”.
- Inside the print function, the var variable is concatenated between “I have “ and “ stamps”.
Output
The code is successfully executed and displays, “I have 20 travel stamps”. The following code displays how a variable and a string can be written on the same line using concatenation in Python:
Method 2: Using Commas
Commas can be used to print the string data along with a variable. The variable is not limited to a specific data structure. The following code explains how commas can be used to print a variable with string data in a similar line in Python:
var1= " electric vehicles."
var2= 5
print('she owns ',var2, var1)
Output
The above code prints “she owns 5 electric vehicles” where it integrates the var1 “electric vehicles”. and var2 having value “5” with “she owns”. The following output displays how a variable can be printed alongside the string using commas inside the print() function in Python:
Method 3: Using f strings
The f string is a concise method to print the variables along with the strings. Before the string, the f keyword is given which tells the print function to evaluate the values of the variables inside the curly brackets {}. This method is also not restricted to any data type. The following code uses the f string to print the strings and variables on a similar line:
var1= "scholarship"
var2= 10
print(f'The {var1} is available in {var2} European countries.')
In the above code,
- The var1 variable is defined to have a value of “scholarship”. The var2 is defined to have a value of 10.
- The print() function has a f keyword before the string with {var1} and {var2} inside it. The values of var1 and var2 are evaluated and printed in the resulting string.
Output
The above code integrates the var1=scholarship and var2=10 into the string data to print “The scholarship is available in 10 European countries”. The following output displays how a variable can be printed alongside the string data using the f keyword before a string in Python:
Method 4: Using .format()
Another method that can get the variable in the same line as the string data is “.format()”. The variables are defined initially. Inside the print() function, the curly brackets are present that are placeholders to the variables that will be passed inside the format() method. The following code uses the “.format()” method to print the variable and string on the same line:
var1= "academy"
var2= 10
print('The naval {} has {} trainers,'.format(var1,var2))
In the above code,
- A variable var1 is defined to have a value of “academy”.
- A second variable var2 is defined to have a value of “10”.
- Inside the print() function, the string contains two curly brackets{}. The “.format” method is invoked on the string with the parameters var1 and var2.
- The values of the variable are evaluated inside the string.
Output
The above code prints “The naval academy has 10 trainers” where var1=“academy” and var2= “10” are evaluated and printed alongside the string data. The following output displays how the variables can be written alongside the string data using the “.format()” method:
Method 5: Using print() + str()
Concatenation only allows variables of string data type to be printed alongside the string data. However, data of different data types can be added to the string data using the str() method along with the concatenation operator. The str() method converts the input value to the string data. The following code explains how using str() along with the concatenation can print the variable and string in the same line in Python:
var1= "fruit basket"
var2= 20
print('The ' + str(var1) + ' contains ' + str(var2) + ' mangoes.')
In the above code,
- The code defines the variable var1 which says “fruit basket”.
- Another variable var2 is defined which has a value of 20.
- The str() method is applied to both variables to ensure that they are in the string format.
- The result is printed.
Output
The above code prints “The fruit basket contains 20 mangoes” where 20 is converted to the string format using the str() method. The following output displays how a variable can be printed along with a string in Python using the str() method:
Method 6: Using % Operator
Using the %s placeholder inside the string and then adding the variable after it with the % operator gets its value. This is useful when handling URLs with the numeric data. The following code explains how the % operator can be used to print the variables along with the string data in Python:
var1= 30
var2= 'bucket list,'
print('In her %s'%var2, 'she has a goal of travelling to %s' %var1, 'countries.')
Output
The following output displays how variables can be printed along the string data using the %s placeholder in Python:
That’s all about printing variables and strings on the same line in Python.
Conclusion
Different methods like “f strings”, “commas”, “%s” operator, “.format()” function, and “+ str()” can be used to print the variables along with strings in the same line in Python. Adding variables of different data types into string data helps in efficient data processing and user interaction. This article has discussed various methods to print the variables along with the string on the same line. All the mentioned methods are explained with relevant examples.