This article introduces you to defining strings in Python. Friends in need can refer to the content in the article
We explained earlier what a string is. Strings can be represented by ''
or ""
.
What if the string itself contains '
? For example, if we want to express the string I'm OK
, we can use <span style="color:rgb(255,0,0);">" "</span>
to express:
"I'm OK"
Similarly, If the string contains "
, we can use ' '
to express:
'Learn "Python" in imooc'
If the string contains both ' What should I do if
also contains "
?
At this time, you need to "escape" some special characters in the string. Use \
to convert the Python string. righteous.
要表示字符串 Bob said "I&#39;m OK".
由于 ' 和 " 会引起歧义,因此,我们在它前面插入一个\
表示这是一个普通字符,不代表字符串的起始,因此,这个字符串又可以表示为
'Bob said \"I\'m OK\".'
注意:转义字符 \ 不计入字符串的内容中。
常用的转义字符还有:
\n 表示换行 \t 表示一个制表符 \\ 表示 \ 字符本身
相关推荐:
The above is the detailed content of Define strings in Python. For more information, please follow other related articles on the PHP Chinese website!