Output Variables
Outputting variables in Python allows you to display the values of variables in your program. You can output variables using the print()
function or by formatting strings with variables.
Using the print()
Function
The simplest way to output variables in Python is using the print()
function. For example:
print('Hello, world!') print('My name is', name) print('I am', age, 'years old')
Output:Hello, world! My name is Alice I am 30 years old
Using String Formatting
You can also format strings with variables using the %
operator or the format()
method. For example:
print('My name is %s' % name) print('I am %d years old' % age) print('My name is {} and I am {} years old.'.format(name, age))
Output:My name is Alice I am 30 years old My name is Alice and I am 30 years old.
Using f-Strings (Python 3.6+)
In Python 3.6 and later versions, you can use f-strings for string formatting, which provide a more concise and readable syntax:
print(f'My name is {name}') print(f'I am {age} years old')
Output:My name is Alice I am 30 years old
Conclusion
Outputting variables in Python allows you to display the values of variables in your program. By using the print()
function and string formatting, you can output variables in various formats and styles to meet your needs.