3. String in Python

Source: Internet
Author: User

First, preface

The string is an important data type in Python. A string is a piece of text that is marked in quotation marks in Python.

Second, string classification

Strings are divided into 3 categories depending on the usage scenario:

(1) Single-quote, double-quotation-mark-created single- line string;

In single-quote and double-quotation-mark single-line strings, you can't wrap a return key in the Python shell IDE, and the command will be executed directly in some editors, with a single-line string of two lines. If you want to wrap lines in a single-line string, you can only enter \ n in a string, but this is not recommended.

Example 1:

Example 2:

Here in the position of the cursor line, directly to help you open a new line string prompt you to enter, in line-break except for the \ to splice two strings. But the actual output a is "" "Hello world! , there is no line break.

(2) r/r "" creates the original string.

\ is a python operator, called the escape character, the appearance of this symbol indicates that the character followed by is not the original meaning, there is a special meaning. For example, \ n in a string is a newline display. If we do want to display \ n These two characters in a string, it is necessary to add a \ in front of \ To illustrate that the second \ is not trying to escape N, such as \\n.

But if there's too much in a string to escape, it can be cumbersome, and we'll be able to use the original string.

R/R "" means that the characters in the quotation marks have no special meaning, so what they do, \ is just a backslash.

The most common scenario for raw strings is to write regular expressions and use strings to represent our directory (Windows).

Speaking of \, here to say the end of the \ Why can be stitched two lines of command. The program we write is also a text, the return key is a control, but it shows that the effect on the screen is a newline, if there are two lines that should be connected to the line display, in the middle of the screen is more than a line break, then in front of the line break add a \, as if with the computer said that this symbol is used for line display , you don't have to do it as an order. In other words, the line break is logically canceled. (Personal understanding)

(3) Three quotation marks create a newline string.

It's not a smart idea to create multiline strings with single quotes and double quotes, but we should use three quotes when we do want to create multiple lines of string. Multi-line strings are commonly used to describe documents for re-functions, classes, and modules.

Three, the string commonly used methods.

Str.strip ()/lstrip ()/rstrip (): Removes whitespace from both ends/left/right of the string.

Str.split (STR1): Takes the str1 as a delimiter, divides str into several strings, and returns a list of these strings as elements.

Str.join (seq): SEQ is a list of strings as elements, with Str as the concatenation character, to stitch strings in a list into a string.

Str.replace (STR1,STR2): In str, replace all str1 with str2.

Str.format (x): string formatting.

Str.startswith (Strx)/endswith (STRX): Check if STR starts/ends with Strx.

—————— the end of this article!

3. String in Python

Related Article

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.