Basic introduction to Python 0-------str string

Source: Internet
Author: User
Tags first string

A very high frequency data type "str string" is used in programming, and the string belongs to an immutable sequence.

It is possible to declare a string in Python using ' single quotation marks ' or ' double quotation marks '.

A string that matches the quotation mark before and after

The following is the implementation of a multiline string: three quotation marks "" "Here is the multiline text" ""

The direct output sees \ n represents a newline character, followed by a string of characters that are called the escape character "escape character" personal understanding: denotes those special characters that are not displayed on the computer "

Print () Prints the text that is displayed, and you can see the information for the line break.

The escape character sometimes affects our strings, such as when we have a folder under C.

For example, the Test folder under the C drive , (\ t is an escape character) is automatically escaped when it is output. At this point, we need to ignore the escape character. We 'll put an R in front of the string to ignore the escape character.

Declare the string in bytes, preceded by a B

However, when displayed in Chinese, it is not possible to throw an error message "The byte can only contain ASCII text characters." ”

But we can use the Encode () method. The decode () method is re-converted to the original string

Common operations for strings. Since it says that it is an immutable sequence, he also supports some common operations of sequences

(1) Determining whether a character is inside a variable

(2) Join string

(3) Repeating string

(4) Get characters by subscript

(5) Len (str) gets the string length

(6) Max (str) gets the maximum character

(7) min (str) gets the smallest character "note: the size of Max and Min matches is based on the pre-and post-comparison of the ASCII table."

(8) Index () retrieves the position of the first occurrence of the string

(9) Count () counts the number of occurrences of a character

Common Operations for strings

(1) STR conversion operation

When one of our types of STR data is spliced with an int type, an error "cannot be implicitly converted to an int" is generated. We need to use the conversion function str ()

(2) Replace () string substitution

The string itself is immutable, assuming we need to modify a to a, the following error will be generated using the following notation.

The ' ABCDEFG ' a character is relatively small. We can make a flexible implementation, excluding the first string, using the stitching in front of a

If the same character in a string appears more, the replacement content is more, then the above method is not very flexible, this time we use the Replace () function

Replace the original string does not change, replace needs a new variable to receive

Replace (OLDSTR, NEWSTR, [Count]) replace the third parameter count is optional, the count parameter mainly indicates how many characters need to be replaced

(3) Capitalize () capitalizes the first letter of a string

(4) Upper () converts a string to uppercase

(5) lower () converts a string to lowercase

(6) StartsWith (str) determines what the string begins with. return bool Value

(7) EndsWith (str) determines what the end is. return Bool value

(8) IsNumeric () determines whether a string is a number

(9) Isalpha () determine if the string is in English

Split (str) splits a string by the specified character. Returns an array of strings

(one) Join join string

Format formatted string

When we output a very long string, we often use a string join, which is inconvenient, and the length is long, it is hard to tell from there. You can use the Format function at this time.

This is called a placeholder with curly braces that enclose a number in the middle. The advantage of using format is that you don't have to convert numbers

Extension, another way of writing.

Multiple ways to use placeholders

(1) How many character lengths are used for the representation of a value after this position. The characters are left aligned and the numbers are right justified

(2) ' > ' greater than right alignment. ' < ' less than sign left justified

(3) indicated as a floating-point number

Represents the display as a two-digit decimal point

Represents a 6-bit occupancy and retains a two-bit decimal point in the form display

The f represents the float type: 2 means that two decimal places are reserved: 2 The preceding 06 represents a 0 position in the form of a fill of 6.

The int type is also possible

Hexadecimal display

Eight-in-binary display

Binary display

Escape Character Encyclopedia

Basic introduction to Python 0-------str 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.