Sei sulla pagina 1di 2

Triple, double and single quotes

Python doesn't care if you use single quotes or double quotes to print a single
statement. But, surely, both has some significance while printing complex
statements, which we will see soon.
Getting Started with Python
[ 23 ]
print "Hello World!" and print 'Hello World!' will give the same output Hello
World! two times:
How will you print something like this:
I am mad in love do you think I am doing the right thing? One way is to enclose
the complete thing within the triple quotes as shown here:
print '''I am mad in love do you think
I am doing the right thing '''
Alternatively, you can also use double quotes three times to achieve the same
thing:
print """I am mad in love do you think
I am doing
the right thing """
Getting Started with Python
[ 24 ]
The preceding two examples are not in formatted form, they are just to show how we
can achieve multiline printing.
Let's try another example. What should be the outcome of the following statement?
print 'Hey there it's a cow' The preceding piece of code gives the following
results:
C:pydev>python hello.py File "hello.py", line 1 print 'Hey there it's a cow' ^
SyntaxError: invalid syntax Python simply interprets that the statement terminated
with a single quote after it. The solution is to enclose the complete sentence
within double quotes as shown:
print "Hey there it's a cow" Adding double quotes (") gives an error-free output as
shown:
C:pydev>python hello.py Hey there it's a cow
Python back slash The Python back slash is used for continuation of the print
statement. You can stretch a single statement across multiple lines:
print "Hello world " This gives the following output:
C:pydev>python hello.py Hello world
Getting Started with Python
[ 25 ]
String inside the quotes
For printing a string, either a pair of single (' ') quotes or pair of double
quotes (" ") can be used as shown in the succeeding examples:
print "Hello World 'Mr' Bond" print 'old world "but" still good' This gives the
following results:
C:pydev>python hello.py Hello World 'Mr' Bond old world "but" still good
Escape sequence in Python The escape sequence is used to insert the tab, the
newline, the backspace, and other special characters into your code. They give you
greater control and flexibility to format your statements and code: Escape Sequence
Meaning b Backspace a Sound system bell n Newline t Horizontal tab The character '
Single quotation mark " Double quotation mark
print 'a' print 'tHermit' print "i know , they are 'great'" The output is as
follows:
C:pydev>python hello.py Hermit i know , they are 'great'
Getting Started with Python
[ 26 ]
The preceding code executes with a beep sound. If you did not hear the beep sound,
check your speakers.
String concatenation Two strings can be joined using the + operator:
print "Only way to join" + "two strings" The following is the output of the
preceding code:
C:pydev>python string_concatenation.py Only way to join two strings
Formatted output Consider an example where you would want to print the name, marks,
and the age of the person:
print "Name", "Marks", "Age" print "John Doe", 80.67, "27" print "Bhaskar", 76.908,
"27" print "Mohit", 56.98, "25" The output will be as follows:
C:pydev>python hello.py Name Marks Age John Doe 80.67 27 Bhaskar 76.908 27 Mohit
56.98 25 You can see the output, but the output that is displayed is not formatted.
Python allows you to set the formatted output. If you have done some coding in C
language, then you should be familiar with %d, %f, %s. In order to represent an
integer %d is used, %f is used for float, and %s is used for string. If you used
%5d, it means 5 spaces. If you used %5.2f, it means 5 spaces and .2 means
precision. The decimal part of the number or the precision is set to 2. Let's use
the formatting on the preceding example:
print "Name Marks Age" print ( "%s %14.2f %11d" % ("John Doe", 80.67, 27)) print
( "%s %12.2f %11d" %("Bhaskar" ,76.901, 27)) print ( "%s %3.2f %11d" %("Mohit",
56.98, 25))
Getting Started with Python
[ 27 ]
The output we get is as follows:
C:pydev>python hello.py Name Marks Age John Doe 80.67 27 Bhaskar 76.90 27 Mohit
56.98 25 The preceding output is much better than the previous one. You can see
Marks 76.901 set to 76.90 automatically.

Potrebbero piacerti anche