Python中的单引号,双引号和三引号有什么区别? [英] What is the difference between single, double, and triple quotes in Python?

查看:235
本文介绍了Python中的单引号,双引号和三引号有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

换句话说,我怎么知道要使用哪个?

In other words, how do I know which one to use?

我知道我何时使用字符串.我会做的

I know when I use strings. I would do

    string = "This is a string"

我什么时候使用''或"""?

When would I use ' ' or """ """?

推荐答案

'...'"..."是等效的.如果字符串中有撇号,则使用"..."会更容易,因此您不必转义撇号.如果字符串中包含引号,则使用'...'会更容易,因此您不必转义引号.

'...' and "..." are equivalent. If you have an apostrophe in the string, it is easier to use "..." so you don't have to escape the apostrophe. If you have quotes in the string, it's easier to use '...' so you don't have to escape the quotes.

三重引号(允许使用变体"""''')允许字符串包含换行符.这些通常用于文档字符串(和其他多行注释,包括注释"代码)以及其他计算机语言(例如HTML和SQL)的嵌入式摘要.

Triple quotes (both varieties, """ and ''' are permitted) allow the string to contain line breaks. These are commonly used for docstrings (and other multi-line comments, including "commenting out" code) and for embedded snippets of other computer languages such as HTML and SQL.

https://docs.python.org/2.0/ref/strings.html

这篇关于Python中的单引号,双引号和三引号有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆