What is a python string? A person needs a language to negotiate with a computer.
stringIs
PythonThe most common data types in the. We can use quotation marks ( 'Or ") to create a string, Creating a string is simple, as long as you assign a value to the variable.
For example:
var1 = ' Hello world! ' VAR2 = "www.python.cn"
The definition of a Python string:
Python accesses a value in a string
python does not support single character types, and single-character is also used as a string in Python.
python access substrings, you can use square brackets to intercept strings, as in the following example:
Instance:
#!/usr/bin/python var1 = ' Hello world! ' var2 = "python runoob" print "var1[0]:", var1[0] print "var2[1:5]:", var2[1: 5]
The result of the above instance execution:
Var1[0]: H
Var2[1:5]: Ytho
Python escape character
Python uses a backslash (\) to escape characters when special characters are needed in characters. As the following table:
Escape character |
Describe |
\ (at end of line) |
Line continuation character |
\\ |
Backslash symbol
|
\' |
Single quotation marks
|
\" |
Double quotes
|
\a |
Bell
|
\b |
BACKSPACE (Backspace) |
\e |
Escape |
\000 |
Empty |
\ n |
Line break |
#!/usr/bin/python#-*-coding:utf-8-*-a = "Hello" b = "python" print "A + B output result:", a + B print "A * 2 output:", A * 2 print "a[1" output: ", a[1] print" a[1:4 "output:", A[1:4] if ("H" in a): print "h in variable a" else:
print "H is not in variable a" if ("M" Not in a): print "M is not in variable a" else: print "m in variable a" print r \ n "Print R ' \ n '
The results of the above program execution are:
A + B output result: Hellopython
A * 2 output result: Hellohello
A[1] Output Result: E
A[1:4] Output Result: ell
H in Variable A
M not in variable a
\ n
\ n
For more information, please visit the PHP Chinese Web Python tutorial section.