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

首页 > 实用技巧 > 作者:YD1662024-01-11 06:55:42

逃逸字符和转义字符,特殊字符转义处理方法(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

栏目热文

文档排行

本站推荐

Copyright © 2018 - 2021 www.yd166.com., All Rights Reserved.