Home > Article > Backend Development > What are Python strings? The definition of Python string and how to escape instances in detail
What is a Python string? People need a language to interact with computers. String is the most commonly used data type in Python. We can use quotation marks (' or ") to create a string. Creating a string is as simple as assigning a value to a variable.
For example:
var1 = 'Hello World!' var2 = "www.python.cn"
Definition of Python string:
Python accesses the value in a string
Python does not support single character types, and single characters are also used as a string in Python.
When accessing substrings in Python, you can use square brackets to intercept the string, as shown in the following example:
Example:
#!/usr/bin/python var1 = 'Hello World!' var2 = "Python Runoob" print "var1[0]: ", var1[0] print "var2[1:5]: ", var2[1:5]
The above example execution result:
##var1[0]: Hvar2 [1:5]: ytho
##Python escape character
When special characters need to be used in characters, python uses backslash (\) to escape the characters. The following table: 以上程序执行结果为: a + b 输出结果: HelloPython 更多相关知识,请访问php中文网Python教程栏目。
转义字符
描述
\(在行尾时)
续行符
\\
反斜杠符号
\'
单引号
\"
双引号
\a
响铃
\b
退格(Backspace)
\e
转义
\000
空
\n
换行
#!/usr/bin/python
# -*- coding: UTF-8 -*-
a = "Hello"
b = "Python"
print "a + b 输出结果:", a + b
print "a * 2 输出结果:", a * 2
print "a[1] 输出结果:", a[1]
print "a[1:4] 输出结果:", a[1:4]
if( "H" in a) :
print "H 在变量 a 中"
else : print "H 不在变量 a 中"
if( "M" not in a) :
print "M 不在变量 a 中"
else : print "M 在变量 a 中"
print r'\n'
print R'\n'
a * 2 输出结果: HelloHello
a[1] 输出结果: e
a[1:4] 输出结果: ell
H 在变量 a 中
M 不在变量 a 中
\n
\n
The above is the detailed content of What are Python strings? The definition of Python string and how to escape instances in detail. For more information, please follow other related articles on the PHP Chinese website!