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
, empty-space separated, without the enclosing parentheses!
my_tuple = (1, 2, 3) print(*my_tuple) # 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) print(str(my_tuple)[1:-1]) # 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
string.join(iterable)method concatenates the elements in the given iterable.
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!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. 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?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming 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.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.