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

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
主站蜘蛛池模板: 乌兰浩特市| 广昌县| 五寨县| 金溪县| 孟连| 甘肃省| 娱乐| 内乡县| 新营市| 泰州市| 阿坝县| 冀州市| 河源市| 兖州市| 湄潭县| 祁门县| 曲靖市| 宁河县| 济阳县| 洛浦县| 永城市| 泰兴市| 邹城市| 双牌县| 宾川县| 建阳市| 南靖县| 东乡族自治县| 达日县| 滨海县| 巩义市| 桐柏县| 深水埗区| 靖西县| 和顺县| 钦州市| 高要市| 沂南县| 枞阳县| 云阳县| 潢川县|