How to Print a Tuple Without Parentheses in Python?

Problem Formulation

Given a tuple of elements. If you print the tuple to the shell using print((1, 2, 3)), the output is enclosed in parentheses like so: (1, 2, 3). But you want the list without brackets like so: 1, 2, 3.

print((1, 2, 3))
# Output: (1, 2, 3)
# Desired: 1, 2, 3

How to print the tuple without enclosing parentheses?

Method 1: Unpacking

The asterisk operator * is used to unpack an iterable into the argument list of a given function. You can unpack all tuple elements into the print() function to print each of them individually. Per default, all print arguments are separated by an empty space. For example, the expression print(*my_tuple) will print the elements in my_tuple, empty-space separated, without the enclosing parentheses!

my_tuple = (1, 2, 3)
# Output: 1 2 3

To master the basics of unpacking, feel free to check out this video on the asterisk operator:

Method 2: Unpacking with Separator

To print a comma-separated tuple without enclosing parentheses, the most Pythonic way is to unpack all tuple values into the print() function and use the sep=', ' argument to separate the tuple elements with a comma and a space. Specifically, the expression print(*my_tuple, sep=', ') will print the tuple elements without parentheses and with a comma between subsequent tuple elements.

my_tuple = (1, 2, 3)
print(*my_tuple, sep=', ')
# Output: 1, 2, 3

You can learn about the ins and outs of the built-in print() function in the following video:

Method 3: Slicing String Representation

Slicing is a concise way to access a subsequence from an original sequence. You can use slicing on the string representation of a tuple to access all characters except the first and last ones—that are the opening and closing parenthesis characters. For example, the expression print(str((1, 2, 3))[1:-1]) prints the tuple as "1, 2, 3" without enclosing parentheses.

my_tuple = (1, 2, 3)
# Output: 1, 2, 3

Feel free to dive into slicing next to boost your coding skills:

Method 4: String Join With Generator Expression

You can print a tuple without parentheses by combining the string.join() method on the separator string ', ' with a generator expression to convert each tuple element to a string using the str() built-in function. Specifially, the expression print(', '.join(str(x) for x in my_tuple)) prints my_tuple to the shell without enclosing parentheses.

my_tuple = (1, 2, 3)
print(', '.join(str(x) for x in my_tuple))
# Output: 1, 2, 3

You can modify the separator string on which you join to customize the appearance of the tuple:

my_tuple = (1, 2, 3)
print('-'.join(str(x) for x in my_tuple))
# Output: 1-2-3
  • The string.join(iterable) method concatenates the elements in the given iterable.
  • The str(object) built-in function converts a given object to its string representation.
  • Generator expressions or list comprehensions are concise one-liner ways to create a new iterable based by reusing elements from another iterable.

You can dive deeper into generators in the following video:

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

Source link

Latest articles

Related articles

Leave a reply

Please enter your comment!
Please enter your name here