Simply pass the tuple into the print function to print tuple values. It will print the exact tuple but if you want to print it with string then convert it to string.
Print value from a tuple in python
Simple python example code creates and prints the tuple.
mytuple = ('A', 'B', 'C', 'D', 'E', 'F', 'G') print(mytuple)
Want to return a specific value from the tuple
To get the first and second elements from tuples.
mytuple = ('A', 'B', 'C', 'D', 'E', 'F', 'G') # First value print(mytuple) # Second value print(mytuple)
Get the element from 1 to 2 index values, it actually slicing a tuple.
mytuple = ('A', 'B', 'C', 'D', 'E', 'F', 'G') print(mytuple[1:3])
Output: (‘B’, ‘C’)
Printing tuple with string formatting in Python
Simple way use str method to print tuple with string.
tup = (1, 2, 3) print("This is a tuple : " + str(tup))
Output: This is a tuple : (1, 2, 3)
How to print a list of tuples with no brackets in Python?
To print each item in your list of tuples, just do:
mytuple = ('A', 'B', 'C') for item in mytuple: print(str(item))
In one line
mytuple = ('A', 'B', 'C') print(','.join([str(i) for i in mytuple]))
Do comment if you have any doubts and suggestions on this Python tuple code.
Note: IDE: PyCharm 2021.1.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.