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

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
主站蜘蛛池模板: 清流县| 山阳县| 浦城县| 钟山县| 寿宁县| 乌海市| 渭南市| 昌乐县| 新安县| 花莲市| 新密市| 兴义市| 潍坊市| 旬阳县| 宜丰县| 霍城县| 五河县| 金昌市| 大连市| 新余市| 凤山县| 新化县| 昌宁县| 武乡县| 横峰县| 阳信县| 成安县| 闻喜县| 通榆县| 大田县| 萝北县| 永吉县| 台州市| 永新县| 杭锦旗| 苍溪县| 新源县| 宁远县| 崇明县| 鹿泉市| 雅江县|