Python string type

Source: Internet
Author: User

When using the Python string type, you will need to learn a lot. Next let's take a look at how to learn it in detail, which will be of great help for better use in the future. I hope you will have some gains.

  • Summary of common Python application scopes
  • How to convert a Python program to an EXE file
  • How to bind a C ++ program to Python
  • Python display UTF-8 Chinese Text specific operation method
  • Main features of Python objects

The sequence in is the real sequence of the ordered set of objects indexed by non-negative integers, which means that the subscript starts from 0 ), it includes strings, Unicode strings, lists, tuples, xrange objects, and buffer objects. We will not describe the last two types. We will describe them later.

Python string type

Python has two types of strings: standard string str) is a single-byte character sequence, Unicode string unicode) is a double-byte character sequence.

Define a standard string str in Python) You can use single quotation marks, double quotation marks, or even three quotation marks, which makes Python input text more convenient than C. For example, if the str content contains double quotation marks, it can be defined by single quotation marks, and vice versa. When a character contains line breaks or other special characters, it can be defined using three quotation marks. This makes it much easier to remember the messy escape characters. Of course, Python also supports escape characters, and the meaning is basically the same as that of C. But since there are simple things to use, who is still looking for trouble?

The following is an example to illustrate the above points:

 
 
  1. Str1 = 'I am "Python" \ N'
  2. Str2 = "I am 'python' \ r"
  3. Str3 = """
  4. I'm "Python ",
  5. <A href = "http://Csharp.com"> you are C # </a>
  6. "# You can get html code and other things directly without special processing 7 print str1, str2, str3

To define a Unicode string in Python, you must add a character u before the quotation marks. For example:

 
 
  1. #-*-Coding: UTF-8 -*-
  2. Print u' I'm passen'

This is not convenient for C #, because the C # string is Unicode by default. If Python is to be improved, combine the two strings into one, this can reduce the trouble for beginners. How many posts on the Internet are asking how Python supports Chinese? The root cause is here ). Note that when UTF-8 encoding is used, the length of a Chinese character in a non-unicode character is 3, while that in gb2312 is 2. See the following code:

 
 
  1. #-*-Coding: UTF-8 -*-
  2. Unicode = u' I'
  3. Str = 'I'
  4. Print len (unicode), len (str)
  5. # Output 1 3 6 7 #-*-coding: gb2312 -*-
  6. Unicode = u' I'
  7. Str = 'I'
  8. Print len (unicode), len (str) # output 1 2

In addition, the Python string type does not have the character type in C #, and the short text is also a string. So pay attention to it, because C # is rarely used as char?

Finally, for the string operation methods, basically C # is available in some Python. Let's look at the Python string type manual and other materials. The only difference is that when extracting a SubString of a string in Python, remember to use the "slice" statement to describe the list and metadata later.) instead of looking for a SubString, see the following example:

 
 
  1. #-*-Coding: UTF-8 -*-
  2. Str1 = u' I'm passen'
  3. Print str1 [2: 4]
  4. # Output 'passens'

The above is a detailed introduction to the Python string type.

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.