Python Tutorial (2.4)--string

Source: Internet
Author: User

In section 2.2, there are string types in Python. In python, strings are enclosed in ' or ', such as ' abc ', ' qwerty ', and so on. Note that ' and ' is not the content of the string.

ASCII (American Standard Code for information Interchange) code

Computers can store numbers, what about the characters? The answer is that you can encode characters so that as long as the numbers are stored, the display turns into characters. Since the computer was invented by Americans, only 128 (not quite certain) characters were initially encoded, called ASCII, in the range of 0~127. For example, the ASCII code for the letter A is 65, and the ASCII code for the letter D is 100.

A 8-bit unsigned integer has a storage range of 0~255, so storing ASCII characters requires only one byte.

In addition, there is an extended ASCII encoding with a range of 0~255. The first 128 characters are the same, and the last 128 are characters with phonetic symbols, such as a and é. This is not described in detail here.

Unicode

Because there is not only English in the world, there are other languages, there are more characters in other languages. Therefore, the ASCII code does not meet the requirements and requires a new character set,--unicode. Unicode contains characters for most languages in the world. For example, the Chinese word "i", the Unicode code is 25105, the word "text" is 25991.

The general character needs 2 bytes to store, and the remote one needs 4 bytes.

String and string constants

Depending on the concept of a character, a string can be redefined as a combination of several characters. The string type in Python is strand is Unicode encoded. Therefore, Unicode characters (such as Chinese) can be used. There are no separate character types in Python.

String constants refer to the immutable strings, such as "Python" and "Globe", as opposed to variables.

Escape character/sequence (escape character/sequence)

The string can contain not only ordinary characters, but also special characters. These special characters need to be represented by special methods, which are called escape characters . In Python, the escape character begins with \. Because the escape character needs to be represented in the code by two characters, it is also called an escape sequence .

In the string, you may want to wrap, you can use the \ n escape character; You may want to tab, and you can either tab directly or use the \ T escape character (the latter is recommended).

For example:

Print ('firstline\nsecond line\nthirdline') First Linesecond Linethird line

With the following:

Print ('first line') Print ('secondline') Print ('thirdline')

or below:

Print ('first line') first line Print ('secondline') Second line Print ('thirdline') Third line

is the same.

The same is true for \ t:

Print ('1\t2\t3\t4\tend')1    2    3    4    End

There are also some characters that are prone to ambiguity, such as ', ' and \ (' and ' will be thought of as the beginning or end of the string, \ will be considered an escape symbol), and the escape sequence \ ', \ ' and \ \ can be used to output:

Print ('youmust use \\\ ', \\\ ' and \\\\ to display \ ', \ ' and \ \. ' youmust use \ ', \ "and \ \ To display '," and \.

Of course, for strings that contain only ', you can use ' enclose ' for strings that contain only '. For example:

Print ('All right', he says. ' "All right", he says. Print ("I ' m OK. " )I ' m OK.

As for the string that has both ' and ', it can be used only with the escape sequence or the original string (which is about to be discussed below).

Raw String ( Raw string)

Python supports the original string. The characters in this string are primitive and not escaped. The escape string is enclosed in R ', for example:

>>> R'this\ does not mean newline"this\\n does not mean newline '

Note, however, that Python does not use print () to display strings without the escape symbol \.

Multi-line string

Sometimes, there are a lot of line breaks in the string that are not convenient, so you can use a multiline string . Multi-line strings are enclosed in "" "or" "" "" "(3 ' or 3") of each side. For example:

Print ("first line ...second line ... thirdline") First Linesecond Linethird Line

Note When you enter a multiline string, the command prompt becomes ... before the string ends.

Add \ To prevent line wrapping at the end of a multiline line string:

Print ("" ... first line ... second line ... third line")
First line
Second line
Third line

String manipulation

Calculate length

Use the Len () function to calculate the string length. For example:

>>> Len ('goldenglobalview')'internationalization '>>> len (s)20>>> len ('Unicode string ')10

Get a character in a string

Use string[index] to get the characters indexed to index:

' Python Tutorial '>>> s[0]' P '>>> s[5]' n '>>> s[12] ' I '

Note that the index starts at 0, the 0 index corresponds to the first character, 1 corresponds to the second, 2 corresponds to a third, and so on.

If the index is too large, an error will occur:

>>> s[100]Traceback (most recent):  "<stdin>"   in <module>indexerror:string index out of range

Note that although you can get a character in a string, you still get the string type, which is only a length of 1.

Stitching string Constants

Two string constants can be directly written together for stitching (concatenation):

' class "' Mate ' ' classmate ' " PYT "" Hon " ' python ' ' Note ' "  Book " ' Notebook '

Note that two strings can be enclosed in a ' surround, one '.

Stitching strings

If it is a variable of type str and a string constant, it cannot be directly written together for stitching:

' class ' ' Mate ' "<stdin> ", line 1    ' mate '^ syntaxerror:invalid               Syntax

You need to use the + operator:

' class ' ' Mate ' ' classmate '

The + operator can also be used between two str variables:

' class ' ' Mate '>>> s1 + s2' classmate '

(not finished)

Python Tutorial (2.4)--string

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.