user4513674
user4513674

Reputation:

How to print formatted statement in three separate lines?

I have a formatted print statement that I want to print in three separate lines. The code is below:

sender = '[email protected]'
recipient = '[email protected]'
subject = 'Hello!'

print('From: {} To: {} subject: {}'.format(sender, recipient, subject))

Currently it prints like this:

From: [email protected] To: [email protected] subject: Hello!

But I want it to print like this:

From: [email protected] 
To: [email protected] 
subject: Hello!

I thought I could get it to print that like if I put a "sep" function in the print statement like this:

sender = '[email protected]'
recipient = '[email protected]'
subject = 'Hello!'

print('From: {} To: {} subject: {}'.format(sender, recipient, subject), sep = '\n')

That did not work though. Any ideas? All help appreciated!

Upvotes: 0

Views: 157

Answers (2)

Kasravnd
Kasravnd

Reputation: 107297

The sep keywors just works for separate string like following :

>>> print('From: {}', 'To: {}', 'subject: {}', sep = '\n')
From: {}
To: {}
subject: {}

For an integrate string just sue new line \n character inside your string :

>>> print('From: {} \nTo: {} \nsubject: {}'.format(sender, recipient, subject))
From: [email protected] 
To: [email protected] 
subject: Hello!

Upvotes: 1

user1245262
user1245262

Reputation: 7505

Using '\n' directly in the print string

a ="aaa"
b="bbb"
c="ccc"
print('From: {}\nTo: {}\nSubject:{}\n'.format(a,b,c))

I got this:

From: aaa
To: bbb
Subject:ccc

In your original setup, you only have a single string, so there is nothing for the separator (i.e. sep) to separate.

Upvotes: 2

Related Questions