Python Forum

Full Version: I there a more efficient way of printing ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I need that my values be printed in a standard way for a data logger and I'm trying to be printed like this :
(1, 2, 3)
It's sounded like a very simple thing for me but I haven't managed to deal whit it.
I looked on the net and I found the sep= function which allows you to separate with a point but I want my values to be printed with parentheses at the beginning and the end, how should I do that?

Since now I've come with that:
print(("("),  x , y, z, (")"), sep=",")
Not very efficient and not even working properly!
Use string formatting in Python 3.6--> we did get f-string.
x = 5
y = 50
z = 999

print(f'{x, y, z}')
Output:
(5, 50, 999)
(Nov-30-2020, 06:21 PM)snippsat Wrote: [ -> ]Use string formatting in Python 3.6--> we did get f-string.
x = 5
y = 50
z = 999

print(f'{x, y, z}')
Output:
(5, 50, 999)

Thank you! I will check this article!
(Nov-30-2020, 08:10 PM)Capitaine_Flam Wrote: [ -> ]
(Nov-30-2020, 06:21 PM)snippsat Wrote: [ -> ]Use string formatting in Python 3.6--> we did get f-string.
x = 5
y = 50
z = 999

print(f'{x, y, z}')
Output:
(5, 50, 999)

Thank you! I will check this article!
I discovered that this function doesn't work on micropython so it's not working for my data logger, I should have mentioned it sorry about this.
Do you know another method that will work on micropython?

I saw this package https://pypi.org/project/future-fstrings/ but I'm not sure how to install it on my micropython chip which is a microbit, they are running a special version.
(Nov-30-2020, 08:34 PM)Capitaine_Flam Wrote: [ -> ]Do you know another method that will work on micropython?
Try with .foramt() method,it should works as it was new in Python 2.6.
x = 5
y = 50
z = 999

print('({}, {}, {})'.format(x, y, z))
# Or
print('{}'.format((x, y, z)))
Output:
(5, 50, 999) (5, 50, 999)
while f-strings and format will allow for much more custom print format, for what you want you can just print the tuple

x = 5
y = 50
z = 999
print((x, y, z))
Output:
(5, 50, 999)
Complete different approach, not only about printing/representation. Requires Python 3.7+
The dataclass decorator makes a nice representation from a class and some other convenient stuff. In addition, usually you want to keep points together in a tuple, a sequence or mapping. If you have more than one point, it's better to keep x, y and z together in one object.

from dataclasses import dataclass

@dataclass
class Point:
    x: int
    y: int
    z: int


point1 = Point(1, 2, 3)
point2 = Point(x=-5, y=10, z=5)

print(point1)
print(point2)
If you want to sort Points by x, y and z, dataclass needs the extra argument order. The Argument frozen will prevent assigning new objects to the Point object. Then the dataclass behaves like a tuple.


from dataclasses import dataclass

@dataclass(order=True, frozen=True)
class Point:
    x: int
    y: int
    z: int


point1 = Point(1, 2, 3)
point2 = Point(x=-5, y=10, z=5)

sorted_points = sorted([point1, point2])
print(sorted_points)
Instead of dataclass same can be achieved with collections.namedtuple