All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Python Print String And Int On Same Line

Last Updated : Mar 11, 2024

Python Print String And Int On Same Line

In this tutorial we will show you the solution of python print string and int on same line, so let’s suppose we were printing two strings or variables. When we run the print function twice, we will notice that it automatically prints them on separate lines.

This is because the print function comes with a parameter named “end” whose default value is “/n”, the newline character in Python. So let’s start.

Step By Step Guide On Python Print String And Int On Same Line :-

You may think that if we go ahead and put the print statements on the same line and separate them with a comma, that maybe it will put both of them on the same line however, this actually doesn’t.

In Python, you can use a comma "," to separate strings and variables when printing an int and a string on the same line, or you can convert the int to a string.

There are many ways to print int and string in one line. Some methods are:-

  • Using the “%” format specifier
  • Using comma
  • Using the format() function
  • Convert int to string and concatenate them
intValue = 25
stringValue = 'years old'
print('I am %s %s' % (intValue, stringValue))
print('I am', intValue, stringValue)
print('I am {} {}'.format(intValue, stringValue))
print('I am ' + str(intValue) + ' ' + stringValue)
  1. We are going to use each and every method that can be helpful to you to display a string and an int on the same line.
  2. So, in the first line, there is a variable called “intValue” that has a value of “25” assigned to it. This variable is of the integer data type.
  3. In the next line, there is a variable named “stringValue” that has a value of “years old” assigned to it and it is of the string data type.
  4. If you are familiar with the C programming language, then you may know what the “%” format specifier is used for. It is used to define the type of data to be printed on standard output.
  5. The next line contains a print statement that resembles the printf() function from the C programming language. This print statement will display the values of both the variables converted into strings on the same line.
  6. In the next approach, we will use the comma to print in same line. What you can do is just put a comma between any number of variables and it will get the work done. The next print statement is the best example of that.
  7. The print statement has 3 parameters: the first one is the string saying “I am”, the second one is the variable “intValue”, and the last one is the variable “stringValue”.
  8. In the next approach, we will use the format() function to display the whole string on the same line.
  9. The print statement has the string and a “.” operator to call the format() function that has both the variables as parameters. The {} brackets represent the variables passed in as the parameters.
  10. In the last print statement, we will perform concatenation with the help of the “+” operator and perform type casting from an integer to a string as well. The print statement has the string, then the “+” operator, and then the type casting of the value of the variable “intValue”.
  11. After that, there is a white space between two “+” operators and then, last, the variable “stringValue”. All the print statements will display the same sentence “I am 25 years old” in the output window.

Conclusion :-

So finally, in conclusion, we can say that with the help of this article, you can now print a string value and an integer value on the same line in a Python program.

The best and simplest way is to use a comma. But you can apply any of the methods mentioned above.

I hope this tutorial on python print string and int on same line helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪