Maxim Yefremov
Maxim Yefremov

Reputation: 14185

print tuple beautifully with newline

>>> print(("hello\nworld", "hello2"))
('hello\nworld', 'hello2')

How to make it print:

('hello
world', 'hello2')

I mean it must not print \n as the symbol but implement this symbol and make a new line.

Python version is 3.4.

I tried to use pprint but it does the same:

>>> import pprint
>>> pp = pprint.PrettyPrinter(indent=4)
>>> pp.pprint(("hello\nworld"))
'hello\nworld'

Upvotes: 3

Views: 11537

Answers (6)

vijay9908
vijay9908

Reputation: 57

You can do something like this:

v = [(1,2),(2,3),(4,5)]
for item in v:
    n1,n2 = item
    print(n1,n2)

Refer to this https://i.sstatic.net/rEfiM.png

Upvotes: 0

Bruno Bronosky
Bruno Bronosky

Reputation: 70539

Here is a more complex example from some Ansible output that was annoying me:

import pprint

f={
    "failed": True,
    "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'dict object' has no attribute 'uid'\n\nThe error appears to have been in '/usr/local/etc/ansible/roles/singleplatform-eng.users/tasks/main.yml': line 7, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Per-user group creation\n  ^ here\n"
    }

def ppdump(data):
    print pprint.pformat(data, indent=4, width=-1).replace('\\n', '\n')

ppdump(f)
{   'failed': True,
    'msg': "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'dict object'
has no attribute 'uid'

The error appears to have been in '/usr/local/etc/ansible/roles/singleplatform-eng.users/tasks/main.yml': line 7, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


- name: Per-user group creation
  ^ here
"}

The problem is that pprint escapes out newlines, so I just unescape them.

Upvotes: 0

Open AI - Opting Out
Open AI - Opting Out

Reputation: 24164

>>> t = ("hello\nworld", "hello2")
>>> print '({})'.format(', '.join("'{}'".format(value) for value in t))
('hello
world', 'hello2')

This won't be correct if the strings contain ' marks.

Be aware that Python's formatting does clever things to deal with strings containing quotation marks.

Upvotes: 0

Blckknght
Blckknght

Reputation: 104852

There isn't anything that will do that kind of printing for you automatically. Python containers by default use repr to convert their contents to strings (even when you call str on the container, rather than repr). This is to avoid ambiguity from things like ["foo, bar", "baz"] (if the quotes didn't get included, you couldn't tell if there were two or three items were in the list).

You can do your own formatting of your tuple, however, and get the output you want:

print("({})".format(", ".join(tup)))

Upvotes: 2

TigerhawkT3
TigerhawkT3

Reputation: 49330

If you didn't want the parentheses and commas, it would be a simple matter of using the * operator:

>>> t = ("hello\nworld", "hello2")
>>> print(*t)
hello
world hello2

If you want it to print the parentheses and commas but also turn '\n' into newlines, you'll have to code that behavior, as @Peter says.

>>> print('(' + ', '.join(t) + ')')
(hello
world, hello2)

Upvotes: 1

Jeremie
Jeremie

Reputation: 280

Writing:

print("('Hello\nworld', 'hello2')")

will literally print:

('hello
world', 'hello2')

If you are just wanting a new line inserted in your string, use this:

print("Line1\nLine2")

the \n is the escape sequence for a new line, terminating the current line and signaling the start of the next line.

For comparing it to the code you have, you should note the placement of the " symbols, denoting the beginning and end of the string.

Upvotes: 0

Related Questions