Python Variables :Output Variables
In Python, you can display the values of variables using several methods, mainly through the print() function. Here’s how you can output variables:
1. Basic Output Using print()
You can use the print() function to output one or more variables. The variables can be strings, numbers, or other data types.
name = "Alice"
age = 25
print(name) # Output: Alice
print(age) # Output: 25
2. Output Multiple Variables in print()
You can print multiple variables by separating them with commas inside the print() function. Python will insert spaces between them by default.
name = "Alice"
age = 25
print("Name:", name, "Age:", age) # Output: Name: Alice Age: 25
3. String Concatenation in print()
You can concatenate strings and variables by using the + operator, but this requires the variables to be of the str type. Use str() to convert other types like integers.
name = "Alice"
age = 25
print("Name: " + name + ", Age: " + str(age)) # Output: Name: Alice, Age: 25
4. Formatted String Literals (f-strings)
In Python 3.6 and later, you can use f-strings (formatted string literals) to embed variables inside a string by using {} placeholders.
name = "Alice"
age = 25
print(f"Name: {name}, Age: {age}") # Output: Name: Alice, Age: 25
5. Using format() Method
Another way to format strings is by using the format() method, which allows you to insert variables in a string by placing placeholders {}.
name = "Alice"
age = 25
print("Name: {}, Age: {}".format(name, age)) # Output: Name: Alice, Age: 25
You can also specify the order of variables using numbers inside the placeholders:
print("Age: {1}, Name: {0}".format(name, age)) # Output: Age: 25, Name: Alice
6. Using Percentage Formatting (Old Style)
In older versions of Python, you can use % to format strings. It works similarly to the printf function in C.
name = "Alice"
age = 25
print("Name: %s, Age: %d" % (name, age)) # Output: Name: Alice, Age: 25
%sis used for strings.%dis used for integers.
7. Controlling Output with Escape Sequences
You can use escape sequences to control the format of the output. For example, \n adds a new line, and \t adds a tab space.
name = "Alice"
age = 25
print("Name:\t", name, "\nAge:\t", age)
# Output:
# Name: Alice
# Age: 25
8. Customizing the Separator and End Parameters in print()
You can customize the separator between multiple variables and the ending character with the sep and end parameters.
Example using sep:
x, y, z = 5, 10, 15
print(x, y, z, sep=" - ") # Output: 5 - 10 - 15
Example using end:
print("Hello", end=", ")
print("World!") # Output: Hello, World!
9. Printing Variables with a Dictionary or List
If you have a collection like a list or dictionary, you can print it directly:
fruits = ["apple", "banana", "cherry"]
print(fruits) # Output: ['apple', 'banana', 'cherry']
person = {"name": "Alice", "age": 25}
print(person) # Output: {'name': 'Alice', 'age': 25}
10. Combining Multiple Formatting Methods
You can combine f-strings, format(), and other formatting methods for complex output requirements.
name = "Alice"
age = 25
print(f"Name: {name}, Age: {age}".upper()) # Output: NAME: ALICE, AGE: 25
These methods give you flexibility and control over how you output variables in Python!