热线电话:13121318867

登录
2019-02-14 阅读量: 666
Python中的转义排序是什么

打印带有单引号和双引号的字符串会导致语法错误,因为字符串已包含单引号和双引号,因此无法使用其中任何一个进行打印。因此,要打印这样的字符串,要么使用Triple Quotes,要么使用Escape序列来打印这样的字符串。
转义序列以反斜杠开头,可以有不同的解释。如果使用单引号表示字符串,则必须对字符串中存在的所有单引号进行转义,并对Double引号执行相同的操作。
要忽略String中的转义序列,使用rR,这意味着字符串是原始字符串,其中的转义序列将被忽略。

# Python Program for

# Escape Sequencing

# of String

# Initial String

String1 = '''I'm a "Geek"'''

print("Initial String with use of Triple Quotes: ")

print(String1)

# Escaping Single Quote

String1 = 'I\'m a "Geek"'

print("\nEscaping Single Quote: ")

print(String1)

# Escaping Doule Quotes

String1 = "I'm a \"Geek\""

print("\nEscaping Double Quotes: ")

print(String1)

# Printing Paths with the

# use of Escape Sequences

String1 = "C:\\Python\\Geeks\\"

print("\nEscaping Backslashes: ")

print(String1)

# Printing Geeks in HEX

String1 = "This is \x47\x65\x65\x6b\x73 in \x48\x45\x58"

print("\nPrinting in HEX with the use of Escape Sequences: ")

print(String1)

# Using raw String to

# ignore Escape Sequences

String1 = r"This is \x47\x65\x65\x6b\x73 in \x48\x45\x58"

print("\nPrinting Raw String in HEX Format: ")

print(String1)

0.0000
1
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子