What Is Unformatted Output in Python?
The print() function in Python is used to display information on the screen. Whatever you write inside the print() statement is printed as it is. When you print a mix of text and variable values without using specific formatting methods, it’s called unformatted output.
Let’s understand this with an example. Suppose we want to display the population of a country.
We’ll declare two variables:
- country = "India"
- population = 130
Using the print() function, we can write:
python
CopyEdit
print("Population of", country, "is", population, "crores")
This combines both string and variable values using commas. The commas act as concatenation operators, meaning they join different elements together for display.
But if we use the + symbol to concatenate:
python CopyEdit
print("Population of " + country + " is " + population + " crores")
This will raise an error because population is an integer, and you can’t
concatenate a string with an integer directly using +
To fix this, we need to convert the integer to a string using the str() function:
python CopyEdit
print("Population of " + country + " is " + str(population) + " crores")
This process is called typecasting or type conversion.
Sample Python Code
Code Explanation
- country and population store the required data.
- The first print() uses commas to concatenate string and variable values.
- The second print() uses + symbols, but the population value is converted using str().
- str() is used for typecasting an integer to a string so it can be concatenated.
- Both methods produce the same output but handle data differently.
Output
Population of India is 130 crores
Population of India is 130 crores
Watch the Full Video
Check out our YouTube session for a full walkthrough of this topic and a demo of the code: Watch on YouTube
Final Words
Today you learned about unformatted output and how to concatenate strings and variables using both commas and plus signs. Practice well using these examples and be ready for our next session, where we’ll cover formatted output in Python.