python 原始字符串

Python raw string is created by prefixing a string literal with ‘r’ or ‘R’. Python raw string treats backslash (\) as a literal character. This is useful when we want to have a string that contains backslash and don’t want it to be treated as an escape character.

Python原始字符串是通过在字符串文字前加上'r'或'R'来创建的。 Python原始字符串将反斜杠(\)视为文字字符。 当我们想要一个包含反斜杠的字符串并且不希望将其视为转义字符时,这很有用。

Python原始字符串 (Python Raw String)

Let’s say we want to create a string Hi\nHello in python. If we try to assign it to a normal string, the \n will be treated as a new line.

假设我们要在python中创建字符串Hi \ nHello 。 如果我们尝试将其分配给普通字符串,则\ n将被视为新行。

s = 'Hi\nHello'
print(s)

Output:

输出:

Hi
Hello

Let’s see how raw string helps us in treating backslash as a normal character.

让我们看看原始字符串如何帮助我们将反斜杠视为普通字符。

raw_s = r'Hi\nHello'
print(raw_s)

Output: Hi\nHello

输出: Hi\nHello

Let’s see another example where the character followed by backslash doesn’t have any special meaning.

让我们看另一个示例,其中后面跟反斜杠的字符没有任何特殊含义。

>>> s = 'Hi\xHello'File "<input>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \xXX escape

We got the error because python doesn’t know how to decode ‘\x’ as it doesn’t have any special meaning. Let’s see how we can create the same string using raw strings.

我们收到错误消息是因为python不知道如何解码'\ x',因为它没有任何特殊含义。 让我们看看如何使用原始字符串创建相同的字符串。

>>> s = r'Hi\xHello'
>>> print(s)
Hi\xHello
>>> r'Hi\xHello'
'Hi\\xHello'

Don’t get confused with the output having two backslashes. It’s just to show it as a normal python string where backslash is being escaped.

不要混淆具有两个反斜杠的输出。 只是将其显示为转义了反斜杠的普通python字符串 。

Python Raw字符串和引号 (Python Raw String and Quotes)

When a backslash is followed by a quote in a raw string, it’s escaped. However, the backslash also remains in the result. Because of this feature, we can’t create a raw string of single backslash. Also, a raw string can’t have an odd number of backslashes at the end.

当反斜杠后跟原始字符串中的引号时,将对其进行转义。 但是,反斜杠也保留在结果中。 由于此功能,我们无法创建单反斜杠的原始字符串。 另外,原始字符串的末尾不能有奇数个反斜杠。

Some of the invalid raw strings are:

一些无效的原始字符串是:

r'\'  # missing end quote because the end quote is being escaped
r'ab\\\'  # first two backslashes will escape each other, the third one will try to escape the end quote.

Let’s look at some of the valid raw string examples with quotes.

我们来看一些带引号的有效原始字符串示例。

raw_s = r'\''
print(raw_s)raw_s = r'ab\\'
print(raw_s)raw_s = R'\\\"'  # prefix can be 'R' or 'r'
print(raw_s)

Output:

输出:

\'
ab\\
\\\"

That’s all for a quick introduction of python raw string.

这就是快速介绍python raw string的全部内容。

GitHub Repository.GitHub存储库中检出完整的python脚本和更多Python示例。

翻译自: https://www.journaldev.com/23598/python-raw-string

python 原始字符串

python 原始字符串_Python原始字符串相关推荐

  1. 以30字符宽居中输出python字符串_python基础--字符串

    Python3 字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号(' 或" )来创建字符串. 创建字符串很简单,只要为变量分配一个值即可.例如: var1 = 'Hell ...

  2. python中return输出字符串_python基础——字符串

    Python 字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可.例如: var1 = 'Hello W ...

  3. 用python倒序输出一个字符串_Python 反转字符串(reverse)的方法小结

    前段时间看到letcode上的元音字母字符串反转的题目,今天来研究一下字符串反转的内容.主要有三种方法: 1.切片法(最简洁的一种) #切片法 def reverse1(): s=input(&quo ...

  4. python基本字符_Python基本字符串,基础,之

    一:字符串 很多人初学编程时,总是担心自己数学不行,潜意识里认为数学好才能编程.实际上,大多数程序员打交道最多的是"字符串"而不是"数字".因为,编程是用来解决 ...

  5. python 输出引号_python输出字符串单双引号如何选择

    在Python当中字符串的输出既可以使用单引号,也可以使用双引号,使用单引号或双引号是没有区别的:当使用双引号将输出的字符串括起来时,内部也可使用单引号,将单双引号匹配正确即可. 在Python中我们 ...

  6. python判断字符_Python判断字符串是否为字母或者数字(浮点数)

    str为字符串s为字符串 str.isalnum() 所有字符都是数字或者字母 str.isalpha() 所有字符都是字母 str.isdigit() 所有字符都是数字 str.isspace() ...

  7. python单词反转_python文本 字符串逐字符反转以及逐单词反转

    python文本 字符串逐字符反转以及逐单词反转 场景: 字符串逐字符反转以及逐单词反转 首先来看字符串逐字符反转,由于python提供了非常有用的切片,所以只需要一句就可以搞定了 >>& ...

  8. python无效的类字符串_Python基础-字符串处理

    ​你好,我是goldsunC 让我们一起进步吧! 字符串操作与处理 字符串是编程语言中经常会遇见的东西,而字符串又和那些编程语句什么的不太一样,我们可能需要对一个字符串进行各种各样的处理,后面我将给出 ...

  9. python中查找字符串_python中字符串操作--截取,查找,替换

    python中,对字符串的操作是最常见的,python对字符串操作有自己特殊的处理方式. 字符串的截取 python中对于字符串的索引是比较特别的,来感受一下: s = '123456789' #截取 ...

  10. python语言字符串_python中字符串的常见操作方法

    原博文 2019-09-06 09:49 − 1. 字符串概念,字符串是一个容器,包含若干个字符并按照一定的顺序组织成一个整体.字符串支持索引操作. 2. 创建字符串基本语法 变量名 = " ...

最新文章

  1. Python之io概念
  2. 方法级别权限控制-基本介绍与JSR250注解使用
  3. 对int array进行排序
  4. 手机远程linux桌面,centos8安装xrdp远程桌面,Android手机连接linux桌面
  5. RabbitMQ消息订阅与轮询
  6. 【排队模型】基于粒子群优化核酸检测排队问题附matlab代码
  7. 斗牛(牛牛)概率计算器
  8. 手机上的APP都是用什么编程语言写的?
  9. linux 符号执行,[原创]符号执行Symcc与模糊测试AFL结合实践
  10. Qt 编程指南 8 显示静态小图片和动态大图片
  11. Linux服务器性能评估
  12. zonecreate
  13. win10生成https证书步骤
  14. 原创|我为什么不建议你等公司倒闭后,再找工作!
  15. 2018读书清单与情况
  16. method属性值为get提交表单信息,为什么在地址栏不会显示呢。
  17. convert函数用法小结
  18. 全面解析jQuery $(document).ready()和JavaScript onload事件
  19. LTE-PHY物理资源划分(一)
  20. 使用max31865读取PT100温度

热门文章

  1. Objective-C 与JAVA的SHA1/HmacSHA1加密算法实现
  2. 深入理解Java虚拟机2——内存管理机制及工具
  3. 图象和文本的绝对位置(九)
  4. [转载] python list中append()与extend()用法
  5. [转载] python中svm的使用_Python中支持向量机SVM的使用方法详解
  6. [转载] pandas dataframe 提取行和列
  7. 访问被拒绝:“Interop.jmail”
  8. python---之suplot和suplots的区别
  9. (五)基于matchTemplate的图像区域匹配
  10. C++ Primer Plus学习(四)—— string类实践