What is a python string? Python string definitions and how to escape instances in detail

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.