官术网_书友最值得收藏!

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.

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 """
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
主站蜘蛛池模板: 怀远县| 祥云县| 金门县| 梓潼县| 江津市| 河间市| 武宁县| 南康市| 湖州市| 舞钢市| 二手房| 屏山县| 泸州市| 徐汇区| 柳河县| 江川县| 仁化县| 长子县| 墨竹工卡县| 吉隆县| 天津市| 长寿区| 临泽县| 庄河市| 雷山县| 东城区| 宜丰县| 浦县| 藁城市| 邢台市| 武强县| 富锦市| 汤阴县| 昂仁县| 文昌市| 仁化县| 宜章县| 吴堡县| 江源县| 西安市| 威信县|