Last updated: Apr 9, 2024
Reading timeยท5 min
Use the print()
function to print a tuple in Python, e.g.
print(my_tuple)
.
If the value is not of type tuple, use the tuple()
class to convert it to a
tuple and print the result, e.g. tuple([1, 2])
.
my_tuple = ('one', 'two', 'three') # โ print a tuple print(my_tuple) # ๐๏ธ ('one', 'two', 'three') # โ print the first element in a tuple print(my_tuple[0]) # ๐๏ธ 'one' # โ print the last element in a tuple print(my_tuple[-1]) # ๐๏ธ 'three' # โ print a slice of a tuple print(my_tuple[0:2]) # ๐๏ธ ('one', 'two')
We used the print()
function to print a tuple's elements.
The print function
takes one or more objects and prints them to sys.stdout
.
If you have a tuple, you can pass it directly to the print()
function to print
it.
my_tuple = ('one', 'two', 'three') print(my_tuple) # ๐๏ธ ('one', 'two', 'three')
If you need to print an element in the tuple, access the element at its specific index.
my_tuple = ('one', 'two', 'three') # โ print the first element in a tuple print(my_tuple[0]) # ๐๏ธ 'one' # โ print the last element in a tuple print(my_tuple[-1]) # ๐๏ธ 'three' # โ print a slice of a tuple print(my_tuple[0:2]) # ๐๏ธ ('one', 'two')
The syntax for tuple slicing is my_tuple[start:stop:step]
.
The start
index is inclusive and the stop
index is exclusive (up to, but not
including).
my_tuple = ('one', 'two', 'three') print(my_tuple[0:2]) # ๐๏ธ ('one', 'two') print(my_tuple[1:3]) # ๐๏ธ ('two', 'three')
0
, and the last element has an index of -1
or len(my_tuple) - 1
.Use the str.join()
method to print a tuple without parentheses.
The str.join()
method will return a string containing the tuple's elements
without parentheses, with a comma separator.
# โ print a tuple of strings without parentheses tuple_of_str = ('one', 'two', 'three') result = ','.join(tuple_of_str) print(result) # ๐๏ธ 'one,two,three'
We used the str.join()
method to print a tuple without parentheses.
The str.join() method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.
Note that the method raises a TypeError
if there are any non-string values in
the iterable.
If your tuple contains numbers or other types, convert all of the values to
string before calling join()
.
tuple_of_int = (1, 2, 3) result = ','.join(str(item) for item in tuple_of_int) print(result) # ๐๏ธ '1,2,3'
The example uses a generator expression to convert each integer in the tuple to a string.
The string the join()
method is called on is used as the separator between the
elements.
my_tuple = ('one', 'two', 'three') my_str = ', '.join(my_tuple) print(my_str) # ๐๏ธ "one, two, three"
If you don't need a separator and just want to join the iterable's elements into
a string, call the join()
method on an empty string.
my_tuple = ('one', 'two', 'three') my_str = ''.join(my_tuple) print(my_str) # ๐๏ธ "onetwothree"
If you need to print the tuple's elements without parentheses and separated by
spaces, call the str.join()
method on a string containing a space.
my_tuple = ('one', 'two', 'three') my_str = ' '.join(my_tuple) print(my_str) # ๐๏ธ "one two three"
If you need to print a list of tuples without brackets and parentheses, use 2
calls to the str.join()
method.
list_of_tuples = [(1, 2), (3, 4), (5, 6)] result = ','.join(','.join(str(item) for item in tup) for tup in list_of_tuples) print(result) # ๐๏ธ '1,2,3,4,5,6'
The inner call to the join()
method joins the items of the tuple of the
current iteration.
We used the str() class to convert each number to a string.
The last step is to use the join()
method to join the tuples in the list into
a string with a comma separator.
Use a formatted string literal to print a tuple with string formatting.
my_tuple = ('a', 'b', 'c') result = f'Example tuple: {my_tuple}' print(result) # ๐๏ธ Example tuple: ('a', 'b', 'c')
The example uses a formatted string literal to print a tuple with string formatting.
f
.my_str = 'is subscribed:' my_bool = True result = f'{my_str} {my_bool}' print(result) # ๐๏ธ is subscribed: True
Make sure to wrap expressions in curly braces - {expression}
.
You can use bracket notation to access a tuple element at an index.
my_tuple = ('a', 'b', 'c') result = f'first: {my_tuple[0]}, second: {my_tuple[1]}, third: {my_tuple[2]}' print(result) # ๐๏ธ first: a, second: b, third: c
0
, and the last element has an index of -1
or len(my_tuple) - 1
.str.format()
Alternatively, you can use the str.format()
method.
my_tuple = ('a', 'b', 'c') result = 'Example tuple: {}'.format(my_tuple) print(result) # ๐๏ธ Example tuple: ('a', 'b', 'c') result = 'first: {}, second: {}, third: {}'.format(*my_tuple) print(result) # ๐๏ธ first: a, second: b, third: c
The str.format() method performs string formatting operations.
The string the method is called on can contain replacement fields specified
using curly braces {}
.
format()
method as you have replacement fields in the string.You can use the iterable unpacking operator to unpack the tuple's elements in
the call to the format()
method if you need to access them in the string.
my_tuple = ('a', 'b', 'c') result = 'first: {}, second: {}, third: {}'.format(*my_tuple) print(result) # ๐๏ธ first: a, second: b, third: c
The * iterable unpacking operator enables us to unpack an iterable in function calls, in comprehensions and in generator expressions.
The iterable unpacking operator unpacks the tuple and passes its elements as
multiple, comma-separated arguments in the call to the str.format()
method.
You can learn more about the related topics by checking out the following tutorials: