(Python) Using the string .format() method

Python 2

Single

print ‘This is a string : {p}’ . format(p=’example’)

Output : This is a string : example

Multiple

print ‘This is a Object 1 : {a}, Object 2 : {b}, Object 3 : {c}’ . format (a = 1, b  = ‘two’, c = 3.0)

Output : This is a Object 1 : 1, Object 2 : two, Object 3 : 3.0

Python 3 – In Python 3, print works as a function. Therefore, need to place same structure in the ( ).

print (‘This is a Object 1 : {a}, Object 2 : {b}, Object 3 : {c}’ . format (a = 1, b  = ‘two’, c = 3.0))

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s