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

  • Learn Python in 7 Days
  • Mohit Bhaskar N. Das
  • 253字
  • 2021-07-09 20:40:21

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
主站蜘蛛池模板: 佛学| 临武县| 屯留县| 邯郸市| 灌南县| 阿巴嘎旗| 星子县| 东乌珠穆沁旗| 烟台市| 衡南县| 兴义市| 仁化县| 赣榆县| 乡城县| 承德市| 辽宁省| 东莞市| 北京市| 宝应县| 金阳县| 宁津县| 布尔津县| 绥芬河市| 龙江县| 昭平县| 亚东县| 沧州市| 台北县| 新邵县| 阳江市| 神木县| 内丘县| 珲春市| 富蕴县| 敖汉旗| 天等县| 云龙县| 依兰县| 抚宁县| 苍梧县| 镇赉县|