- Python print() function
- 1. Basic understanding of Python print() function
- 2. Printing of multiple objects with Python print() function
- 3. Printing a Tuple and a List with Python print() function
- 4. Python print() function with “sep” keyword
- 5. Python print() function with “end” keyword
- 6. Python print() function with “file” keyword
- Conclusion
- References
- Print a list of tuples in Python
- Method 1: Print list of tuples using for-loop
- Example 1
- Example 2
- Frequently Asked:
- Method 2: Print list of tuples with header row
- Method 3: Print list of tuples using join() function
- Summary
- Related posts:
- Share your love
- Leave a Comment Cancel Reply
- Terms of Use
- Disclaimer
- How To Print Tuple Elements in Python
- Ways to print tuple elements in Python
- Print the entire tuple containing all elements
- Use for loop to print tuple elements
- Print a single element in the tuple
- Print tuple elements in a desired range
- Summary
Python print() function
Python print() function basically prints the given input or object to the output screen or the corresponding stream file.
print(objects, sep=value, end=end_value, file, flush)
Python print() function Arguments:
Arguments | Description | Required/Optional |
---|---|---|
object(s) | An object or input string | Required |
sep=’value‘ | Specification on how the objects are to be separated. Default separator value is ‘ ‘ | Optional |
end=’end_value’ | Specifies what is to be printed at the end. Default value is ‘\n’ | Optional |
file | It is an object with a write method. Default value is sys.stdout. | Optional |
flush | It is a boolean value that specifies whether the output obtained is flushed (True) or buffered (False). Default value is False. | Optional |
1. Basic understanding of Python print() function
# Passing a single object print("Engineering Discipline")
2. Printing of multiple objects with Python print() function
input = "Safa" print("Result is: ", input) # Contains two objects
3. Printing a Tuple and a List with Python print() function
Python print() function can be used to print Strings, Tuples, Lists, etc. to the output screen.
input_tuple = ("YES", "NO", 200) # Tuple print(input_tuple) input_list = [10,'Apple', 20,'Football', 70] # List print(input_list)
('YES', 'NO', 200) [10, 'Apple', 20, 'Football', 70]
4. Python print() function with “sep” keyword
By default, as you all must have observed, the values in the output are separated by space. But, now the User can customize it by replacing the default value i.e. ‘ ‘ (space) using any symbol or value.
value1 = int(10) value2 = 'Social Science' print(value1, value2, sep='+')
5. Python print() function with “end” keyword
As observed, the default value of the ‘end’ parameter is ‘\n’ i.e. the Python print() functions ends with a newline (‘\n’).
But, now the User can customize it by replacing the default value i.e. ‘\n'(newline) using any symbol or value.
my_list = [10, 20, 0, 32, 56, 78, 90] print('Printing the list. ') for x in my_list: print(x, end='$')
Printing the list. 10$20$0$32$56$78$90$
6. Python print() function with “file” keyword
Python print() function’s file parameter enables user to write to a file. If the mentioned file doesn’t exist, it creates a new file with the specified name and writes the output to it.
input_file = open('Print_function.txt','w') print('Social Science', file = input_file) input_file.close()
Conclusion
Thus, in this article, we have understood the working of Python’s print() function.
References
Print a list of tuples in Python
In this article, we will discuss how to print a list of tuples in Python.
Table Of Contents
Method 1: Print list of tuples using for-loop
Iterate over all tuples in list using a for-loop, and print each tuple in a separate line. To print the contents of each tuple, apply astrik operator on tuple to decouple all elements in it, and pass them to print() function. Let’s see some examples,
Example 1
students = [(11, 'Ritika', 'Delhi'), (12, 'Smriti', 'New York'), (13, 'Shaun', 'London'), (14, 'MARK', 'Tokyo')] for tupleObj in students: print(*tupleObj)
11 Ritika Delhi 12 Smriti New York 13 Shaun London 14 MARK Tokyo
Example 2
students = [(11, 'Ritika', 'Delhi'), (12, 'Smriti', 'New York'), (13, 'Shaun', 'London'), (14, 'MARK', 'Tokyo')] for tupleObj in students: print(tupleObj[0], tupleObj[1], tupleObj[2])
Frequently Asked:
11 Ritika Delhi 12 Smriti New York 13 Shaun London 14 MARK Tokyo
Method 2: Print list of tuples with header row
Suppose we have a list of tuples, and a list of strings. Elements in the second list should be printed as header, and tuples in first list should be printed as rows. For this, first we will print the list of header items ina tabular format. Then we will iterate over all tuples in list, and print the content of each tuple i tabular format. Let’s see an example,
students = [(11, 'Ritika', 'Delhi'), (12, 'Smriti', 'New York'), (13, 'Shaun', 'London'), (14, 'MARK', 'Tokyo')] heading = ['ID', 'Name', 'City'] print(f'') for student in students: print(f'')
ID Name City 11 Ritika Delhi 12 Smriti New York 13 Shaun London 14 MARK Tokyo
Method 3: Print list of tuples using join() function
We can join all the tuples in list into a single string. While joining, we will use the newline character as separator, therefore all the tuples will be in separate lines. Let’s see an example,
students = [(11, 'Ritika', 'Delhi'), (12, 'Smriti', 'New York'), (13, 'Shaun', 'London'), (14, 'MARK', 'Tokyo')] strValue = '\n'.join(str(tupleObj) for tupleObj in students) print(strValue)
(11, 'Ritika', 'Delhi') (12, 'Smriti', 'New York') (13, 'Shaun', 'London') (14, 'MARK', 'Tokyo')
Summary
We learned about different ways to print a list of tuples in Python. Thanks.
Related posts:
Share your love
Leave a Comment Cancel Reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Terms of Use
Disclaimer
Copyright © 2023 thisPointer
To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
How To Print Tuple Elements in Python
Tuple in Python helps you store many elements in a specific variable. It is possible to print tuple elements in Python. In this article, we will go together to learn several ways to do this work.
Ways to print tuple elements in Python
Here is how you print tuple elements in Python.
Print the entire tuple containing all elements
The print() function helps you print any data type, including a tuple with its elements.
[variable] = ([element 1], [element 2, …, [element N]])
You create a [variable] that takes the tuple data type. The [variable] stores many elements of different data types. After that, you need to put your tuple as the parameter of the print() function. The output will be all tuple elements inside a () .
tupItems = ("Learn", "Share", "It") print(tupItems)
Use for loop to print tuple elements
You want to print all tuple elements on the screen without the bracket () . This work is possible, all thanks to the for loop.
for element in [variable]: print(element)
[variable] = ([element 1], [element 2, …, [element N]])
The for loop will visit each element in the tuple and print that out. The printed element is in string format.
tupItems = ("Learn", "Share", "It") for element in tupItems: print(element)
You can use end=» » as the second parameter of the print() function. This will help you print all tuple elements in a single line.
tupItems = ("Learn", "Share", "It") for element in tupItems: print(element, end=" ")
Print a single element in the tuple
Tuple in Python has its elements ordered. Therefore, you can access each element in the tuple through its index . This means you can print any element in the tuple as you desire.
[variable] = ([element 1], [element 2, …, [element N]])
The [variable][index] means the element that has the [index] position in the tuple. Please keep in mind that the index in the tuple starts from 0. For example, if you write [variable][2] , you refer to the third element in the tuple.
tupItems = ("Learn", "Share", "It") print(tupItems[1])
As the “Share” has the index 1 in the tuple tupItems, the output will be:
Print tuple elements in a desired range
This method is the advanced version of the previous way. Since tuple elements in Python are ordered in fixed positions, you can not only print a single element but also many elements in the desired range.
print([variable][index 1]:[index 2])
[variable] = ([element 1], [element 2, …, [element N]])
Please make sure that the [index 2] is bigger than [index 1]. The command above will print a new tuple containing only elements in the range from the [index 1] to the [index 2] of the [variable] .
See the code sample below:
tupItems = ("Learn", "Share", "It") print(tupItems[0:2])
Summary
To summarize, we have shared with you 4 ways to print tuple elements in Python. You can print the entire tuple with () or without () (using loop). Also, there are ways for you to print a single element in the tuple , or many elements in the desired range, by accessing the index of the elements.
I am William Nguyen and currently work as a software developer. I am highly interested in programming, especially in Python, C++, Html, Css, and Javascript. I’ve worked on numerous software development projects throughout the years. I am eager to share my knowledge with others that enjoy programming!