当前位置:首页 > 实用技巧 >

逃逸字符和转义字符(特殊字符转义处理方法)

来源:原点资讯(www.yd166.com)时间:2024-01-11 06:55:42作者:YD166手机阅读>>

逃逸字符和转义字符,特殊字符转义处理方法(1)

在本教程中,您将学习如何正确处理python中的字符串,进行打印,切片和格式化。 Python字符串是一系列字符,一个接一个出现,形成一个有意义的单词或句子。

您可以使用单引号,双引号或三引号创建字符串

str_one = "Say Hello to Python Programming" print(str_one) str_two = 'Say Hello to Python Programming' print(str_two) str_three = ''' Say Hello to Python Programming''' print(str_three) str_four = """ Say Hello to Python Programming""" print(str_four) #PYTHON OUTPUT Say Hello to Python Programming Say Hello to Python Programming Say Hello to Python Programming Say Hello to Python Programming

逃逸字符和转义字符,特殊字符转义处理方法(2)

Python字符串–使用单引号,双引号或三引号创建字符串

注意

三引号字符串用于多行文本。

单引号与双引号没有本质区别。

转义码(Escape Codes)

转义码在编程中经常使用到,它们以反斜杠后跟一个字符的形式表示。 每个转义字符都有特定的用途,例如\ n是换行符,它将之后的字符打印在新行中。 下面提到了一些转义代码:

逃逸字符和转义字符,特殊字符转义处理方法(3)

逃逸字符和转义字符,特殊字符转义处理方法(4)

a = 'This is a python string...from single qoutes' b = "This is a python string...from double qoutes" c = '''This is a python string...from 3 single qoutes on start and finish''' d = """This is a python string...from 3 double quotes on start and finish""" print(a) print(b) print(c) print(d) #Python output This is a python string...from single quotes This is a python string...from double quotes This is a python string...from 3 single quotes on start and finish This is a python string...from 3 double quotes on start and finish访问字符串

字符串也可以像数组一样通过索引访问。

Example

website_name = "TheCodeLearners" print(website_name) print(website_name[0]) print(website_name[1]) print(website_name[2]) print(website_name[3]) print(website_name[4]) print(website_name[5]) print(website_name[6]) print(website_name[7]) print(website_name[8]) print(website_name[9]) print(website_name[10]) print(website_name[11]) print(website_name[12]) print(website_name[13]) print(website_name[14]) #Python OUTPUT TheCodeLearners T h e C o d e L e a r n e r s

将TheCodeLearners字符串以15个字符表示,如果我们从0开始计数,则为位置14,一共15个字符。

print(website_name[15]) #PYTHON ERROR OUTPUT string index out of range转义字符串中的特殊字符

反斜杠\用于转义字符串中的任何特殊字符。

Example

string = "This string with double quotes" e_string = "This string with \"double quotes escaped with a backslash" a_string = 'I don\'t like this' print(string) print(e_string) print(a_string) #PYTHON OUTPUT This string with double quotes This string with "double quotes escaped with a backslash I don't like this

如果想打印字符串到新的一行,使用 \n 即可。

Example

x = 'This will be printed in first line.\nThis will be printed in second line' print(x) #PYTHON OUTPUT This will be printed in first line. This will be printed in second line原始字符串

如果在某些情况下,您想省略特殊字符\反斜杠,则可以通过在字符串的开头放置`r`来使用原始字符串。

Example

string = 'Some File Path \temp\name\read' e_string = r'Some File Path \temp\name\read' print(string) print(e_string) #Python OUTPUT Some File Path emp ead Some File Path \temp\name\read

要显示多行消息,请使用”’“””,然后使用r省略\反斜杠。

字符串串联和格式化

您还可以将字符串与其中的变量连接在一起。

age = 15 print("Jim is " str(age) " years old.") #PYTHON OUTPUT Jim is 15 years old. age = 15 print("Jim is %d years old." % age) #PYTHON OUTPUT Jim is 15 years old. age = 15 print("Jim is {} years old.".format(age)) #PYTHON OUTPUT Jim is 15 years old. age = 15 print("Jim is {new_age} years old.".format(new_age=age)) #PYTHON OUTPUT Jim is 15 years old. age = 15 print(f"Jim is {age} years old.") #PYTHON OUTPUT Jim is 15 years old.

使用join()方法链接字符串

join()方法将列表作为参数,并返回单个串联字符串。

a = "Welcome" b = "to python" c = "programming" string1 = ' ' print(string1.join([a,b,c])) # PYTHON OUTPUT Welcome to python programming # or l = ["Welcome", "to python", "programming"] string2 = ' '; print(string2.join(l)) # PYTHON OUTPUT Welcome to python programming

在上面的示例中,列表中的每个项都使用字符串string2进行连接。

使用join()方法连接字符串的另一个示例

my_list = ["Python", "Django", "Django Queryset"] string = '-->'; print(string.join(my_list)) # PYTHON OUTPUT Python-->Django-->Django Queryset

字符串连接的其他方式和示例。

注意

这个星号运算符可以重复字符串

对多个字符串使用星号运算符

x = 3*'hey-' print(x) #PYTHON OUTPUT hey-hey-hey-

使用加法运算符进行字符串连接

x = 3*'hey-' ' new string joined' print(x) #PYTHON OUTPUT hey-hey-hey- new string joined

将数字和字符串连接在一起

Example

x = 'John has scored ' 72 ' runs in ' 4 ' overs' print(x) #PYTHON ERROR OUTPUT TypeError: Can't convert 'int' object to str implicitly x = 'John has scored ' str(72) ' runs in ' str(4) ' overs' print(x) #PYTHON OUTPUT John has scored 72 runs in 4 overs runs = 70 overs = 4 x = 'John has scored ' str(runs) ' runs in ' str(overs) ' overs' #PYTHON OUTPUT John has scored 70 runs in 4 overs

合并多个字符串

Example

string = ('This is string 1' 'This is string 2' 'This is string 3') print(string) #PYTHON OUTPUT This is string 1 This is string 2 This is string 3

使用Modulus运算符的串联

尽管还有其他方法,但该方法提供了一种简单的方法,因此您可能会发现它更简单。

string1 = 'Jake has won $' #type string price = 100 #type int string2 = ' price in swimming competition' #type string print('%s%d%s'%(string1,price,string2)) #PYTHON OUTPUT Jake has won $100 price in swimming competition

使用format()方法

string1 = 'Jake has won $' #type string price = 100 #type int string2 = ' price in swimming competition' #type string print('{}{}{}'.format(string1,price,string2)) #PYTHON OUTPUT Jake has won $100 price in swimming competition #OR string1 = 'Jake has won $' #type string price = 100 #type int string2 = ' price in swimming competition' #type string print('{string1}{price}{string2}'.format(string1=string1,price=price,string2=string2)) #PYTHON OUTPUT Jake has won $100 price in swimming competition #OR string1 = 'Jake has won $' #type string price = 100 #type int string2 = ' price in swimming competition' #type string print('{string1}{price}{string2}'.format(string1='Jake has won $',price=100,string2=' price in swimming competition')) #PYTHON OUTPUT Jake has won $100 price in swimming competition

使用f字符串

此函数在Python Version 3.6中引入。 因此,python开发人员认识到了其快速字符串形成的功能。

string1 = 'Jake has won $' #type string price = 100 #type int string2 = ' price in swimming competition' #type string print(f'{string1}{price}{string2}') #PYTHON OUTPUT Jake has won $100 price in swimming competition,

栏目热文

转义字符五大定义(转义字符在什么情况下用)

转义字符五大定义(转义字符在什么情况下用)

转义、编码和加密是开发中很常见也很基础的概念。对于初学开发的开发者,可能有时会无法准确的区分着几个词。我们将通过这篇文章...

2024-01-11 06:43:44查看全文 >>

几种常用的转义字符(转义字符和合法转义字符区别)

几种常用的转义字符(转义字符和合法转义字符区别)

字符集(Character Set)为每个字符分配了唯一的编号,我们不妨将它称为编码值。在C语言中,一个字符除了可以用它...

2024-01-11 06:44:31查看全文 >>

转义字符包含几个字符(如何正确使用转义字符)

转义字符包含几个字符(如何正确使用转义字符)

理解转义字符转义字符顾名思议就是转换字符的意义,一个转义字符书写上是两个字符,但只表示一个含义。我们已经使用过”\n”将...

2024-01-11 06:56:26查看全文 >>

如何判断转义字符正确与否(合法转义字符怎么判断)

如何判断转义字符正确与否(合法转义字符怎么判断)

现在不少大学都把C语言作为一门必学的编程语言。C语言考试呢,并不能决定你的实践能力怎么样,他考的很多点,我们不知道,也可...

2024-01-11 06:31:38查看全文 >>

空格转义符图解(哪个转义符代表换行效果)

空格转义符图解(哪个转义符代表换行效果)

转义符,由一个 \ 后面加一个字母或符号组成的具有特殊意义的符号,放在字符串里可以控制字符串的输出格式。仅此而已,所以我...

2024-01-11 06:35:22查看全文 >>

空格的转义字符(空白符号粘贴)

空格的转义字符(空白符号粘贴)

先祝我的读者们周末快乐。前阵子一位读者告诉我,某位大厂HR给他发了我之前做的面试题答案合集。这个消息让我开心了一整天,因...

2024-01-11 07:04:14查看全文 >>

转义字符对输出结果的影响(转义字符和原字符的区别)

转义字符对输出结果的影响(转义字符和原字符的区别)

python常用的数据类型有:整数型,浮点型,字符串,布尔值,空值。整型(int)python的整数包括正整数,0和负整...

2024-01-11 06:29:04查看全文 >>

数据本身有转义字符怎么处理(如何处理字符串中的转义字符)

数据本身有转义字符怎么处理(如何处理字符串中的转义字符)

之前发布的文章提到过关于某自媒体平台(为了避免广告,就不说名字了)的文章发布接口的问题:发布文章之后,预期是直接发布好文...

2024-01-11 07:02:11查看全文 >>

一个转义字符占几个字符(转义字符举例说明)

一个转义字符占几个字符(转义字符举例说明)

下面的程序使用了两个 Unicode 的转义字符,它们是用其十六进制代码来表示 Unicode 字符。那么,这个程序会打...

2024-01-11 06:59:30查看全文 >>

凤九为什么不愿原谅帝君(凤九最后原谅帝君了吗)

凤九为什么不愿原谅帝君(凤九最后原谅帝君了吗)

三生三世枕上书,帝君曾经问过凤九,要如何才能证明一个人对另一个人的真心,而凤九则告诉帝君,凡人有一种方法叫做剖心为证,只...

2024-01-11 06:26:51查看全文 >>

文档排行