Define strings in Python

不言
Release: 2018-04-04 15:57:23
Original
1776 people have browsed it

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"
Copy after login

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

&#39;Learn "Python" in imooc&#39;
Copy after login

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

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

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

常用的转义字符还有:

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

相关推荐:

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!