Python中的print函数及其中单引号双引号区别

您所在的位置:网站首页 js节点方法括号内都要用引号吗 Python中的print函数及其中单引号双引号区别

Python中的print函数及其中单引号双引号区别

2024-07-10 06:57| 来源: 网络整理| 查看: 265

一,Python中的print函数

Python中print函数用法:

print(“hello world!”) hello world!

print(5+8) 13

print(“I love” + “you!”) I loveyou! 注意Python中,print函数后面没有英文分号。

二,单引号,双引号

print(‘Hello world!’) Hello world!

print(“Hello world!”) Hello world! 可以看出双引号,单引号输出字符串时没有区别。当字符串中存在单引号或双引号时,直接打印可能会出错

1,比如:Let‘s go!

方法一:

print(“Let’s go!”) Let’s go! 方法二:使用转义字符反斜杠 “\”

print(‘Let’s go!’) Let’s go! 2,如果字符串中有单引号或者双引号

print(“I love ‘Apple’”) #字符串中有单引号时,只需用双引号

I love ‘Apple’

print(‘I love “Aplle”’) #同理

I love “Aplle” 3,假如要打印有很多反斜杠的字符串:

print(“C:\now”) #\n被当做换行符 C: ow

print(“C:\now”) #加一个反斜杠对 \ 进行转义 C:\now

print(r"C:\new\near\now\no") #当字符串很长时,可以使用原始字符串,只需在前面加上 r C:\new\near\now\no #>>> print(r"C:\new\near\now\no") #当原始字符串最后有反斜杠时,会出错 #SyntaxError: EOL while scanning string literal

print(r"C:\new\near\now\no"+"\") #解决办法,拼接一个反斜杠 C:\new\near\now\no 补充:三引号

利用三引号可以实现输出多行文本

print(""" 1! 22! 333! 4444!""")

1! 22! 333! 4444!



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3