Home  >  Article  >  Backend Development  >  Define strings in Python

Define strings in Python

不言
不言Original
2018-04-04 15:57:231781browse

This article introduces you to defining strings in Python. Friends in need can refer to the content in the article


Definition of strings in Python




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&#39;m OK"

Similarly, If the string contains ", we can use ' ' to express:

&#39;Learn "Python" in imooc&#39;

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'm OK".
由于 ' 和 " 会引起歧义,因此,我们在它前面插入一个\表示这是一个普通字符,不代表字符串的起始,因此,这个字符串又可以表示为

&#39;Bob said \"I\&#39;m OK\".&#39;

注意:转义字符 \ 不计入字符串的内容中。

常用的转义字符还有:

\n 表示换行
\t 表示一个制表符
\\ 表示 \ 字符本身

相关推荐:

Python 常用的PEP8规范及Python小把戏


The above is the detailed content of Define strings in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Function basics (python)Next article:Function basics (python)