Python Learning Note 1-python string

Source: Internet
Author: User
Tags string methods

Strings are important data objects in Python

Python strings are any Python data object enclosed in single quotes, double quotes, or three three single quotes, three double quotes, which can be called a python string

Note A data object that contains a single or double quotation mark cannot be wrapped in the middle of a data object (if you need to wrap a line with an escape character \ n) to contain more than one row in three single quotation marks or three double quotation marks.


One, escape character

In any language, there are escape characters, such as the concept of escape character is to let the characters with special meaning lose special meaning to print in normal form.

For example, we need to print out the path to a file

print ' C:\note\takes.txt '

Execution results are

C:oteakes.txt

This is not the result we want. The escape character is used to make the characters of \ n and \ t These special meanings lose their meaning before they are printed in the correct format.

print ' C:\\note\\takes.txt '

Execution results are

C:\note\takes.txt

You can also add "R" in front of the string to cause special characters in the entire string to lose their special meaning.

Print R ' C:\note\takes.txt '


Second, the string shard operation cursor

The string is an ordered sequence

Index starts from 0 to end with-1

A shard operation contains an index start value but does not contain a terminating value, usually a terminating value minus 1


The string variable [cursor] returns the character corresponding to the current cursor.

string variable [start cursor end cursor Step] Start cursor refers to the beginning of the intercept string the subscript end cursor refers to the end of the Intercept string The index step is the number of intervals of the cursor. Note the result of the interception includes the beginning of the cursor excluding the end cursor, which can be omitted by default to the 0 end cursor, which can be saved by default to the end of the string by default to 1.

A= ' 0123456789 ' Print a[1]print a[-2]print a[2:4]print a[2:8:2]print a[:7:2]print a[2:7:]print a[2::]

Execution results are

182324602462345623456789


Three, the String method

The string method is an action method for strings that has been defined and encapsulated using simplified operations and code rewriting

1. String padding method

Center (Width[,fillchar])

The string is centered within the specified length.

Ljust (Width[,fillchar])

The string is left-justified within the specified length

Rjust (Width[,fillchar])

The string is aligned to the right within the specified length

Zfill (width)

Zfill () is populated with character 0, which is more commonly used when outputting values


Width widths indicate what width the character is centered in. The value includes the length of the string itself, and the method does not take effect if the specified length is less than the length of the string itself

The Fillchar optional parameter indicates what character to fill the blank part by default is a space.

Note If you specify a length that is too large to fit on a line, a newline display affects the display effect.

A= ' 0123456789 ' Print a.center (+, ' * ') print a.ljust ("*") Print a.rjust ("*") print ' 1 '. Zfill (2) print '. Zfill (2)

Execution results are

0123456789********************0123456789************************************************** 01234567890110

Expandtabs ([tabsize])

The tabsize parameter of Expandtabs () defaults to 8. Its function is to convert the Tab tab in the string to the appropriate number of spaces.

Attention

The Tabsize parameter includes all characters from the tab to its previous tab

If the value of tabsize is less than or equal to all characters of the tab to its previous tab, the number of spaces displayed is x*tabsize-all characters of that tab to its previous tab

If the value of tabsize is greater than all characters of the tab to its previous tab, the number of spaces displayed is tabsize-all characters of that tab to its previous tab

A= ' 123\twfdsf\tadfsd ' b= ' 012345678901234567890 ' Print Bprint a.expandtabs (3) Print a.expandtabs (2) Print a.expandtabs ( 4) Print A.expandtabs (7)

Execution results are

012345678901234567890123 wfdsf adfsd123 wfdsf adfsd123 wfdsf adfsd123 wfdsf ADFSD

2. String deletion method

Strip () function family to remove whitespace from both ends of a string

Strip ([chars]) removes the specified symbol at both ends of the string

Rstrip ([chars]) removes the specified symbol at the right end of the string

Lstrip ([chars]) removes the specified symbol from the left end of the string

Chars can save the parameter to indicate that the character to be removed can be more than one character omitted by default to a space.

A= ' ADFASDFF ' Print A.strip () print A.lstrip () print a.rstrip () a= ' **********adfadsfasdfasd********** ' Print A.strip ( ' * ') print a.lstrip (' * ') print a.rstrip (' * ') a= ' *!*!*!*!*!adfadsfasdfasd*!*!*!*! ' Print A.strip (' *! ') Print A.lstrip (' *! ') Print A.rstrip (' *! ')

Execution results are

ADFASDFFADFASDFF ADFASDFFADFADSFASDFASDADFADSFASDFASD********************ADFADSFASDFASDADFADSFASDFASDADFADSFASD Fasd*!*!*!*!*!*!*!*!*!adfadsfasdfasd

3. String Morphing method

Lower () converts a string to lowercase

Upper () converts a string to uppercase

Capialize () Capitalize first letter

Swapcase () Convert between casing

Title () capitalize the first letter of the word (by separating the words by space, not distinguishing the spelling of the words)

A= ' Hello world,python! ' Print A.lower () print a.upper () print a.capitalize () print a.swapcase () print a.title () a= ' Hello world,python! ' Print a.capitalize () print a.title ()

The result of the execution is:

Hello world,python! HELLO world,python! Hello World,python!hello world,python! Hello world,python! Hello world,python! Hello world,python!

4. String Segmentation method

Splitlines ([keepends]) returns a list of strings separated by rows (\ n), keepends, optional arguments, or true to display newline characters (\ n) as false, not displayed, and false by default in a substring split in the list.

Split ([Sep[,maxsplit]) returns a list of strings separated by a character.

Rsplit ([Sep[,maxsplit]]) returns a list of strings separated by a character. (separated from right to left)

Sep, optional parameters, separated by the specified character string, by default, by Space, line, tab and other symbols separated;

Maxsplit, optional parameter, specifies the number of times to delimit, and the default is to separate all strings.

Split and Rsplit are the exact same effect without specifying the Maxsplit parameter.

A= ' Hello World python \ni am comming \nabc123 ' Print a.splitlines () print a.splitlines (True) print a.split () Print a.split (' Print A.split (", 4) print a.rsplit () print A.rsplit (") Print A.rsplit (", 4)

The result of the execution is:

[' Hello World python ', ' I am comming ', ' abc123 '] [' Hello World python \ n ', ' I am comming \ n ', ' abc123 '] [' Hello ', ' world ', ' python ', ' I ', ' am ', ' comming ', ' abc123 '] [' Hello ', ' world ', ' python ', ' \ni ', ' am ', ' comming ', ' \nabc123 '] [' Hello ', ' world ', ' python ', ' \ni ', ' am comming \nabc123 '] [' Hello ', ' world ', ' python ', ' I ', ' am ', ' comming ', ' abc123 '] [' Hello ', ' world ', ' python ', ' \ni ', ' am ', ' comming ', ' \nabc123 '] [' Hello World python ', ' \ni ', ' am ', ' comming ', ' \nabc123 ']

5. String Connection method

Join (SEQ) inserts the value of the string into the string provided by the SEQ parameter

The high efficiency of the Join () function (as opposed to the loop addition) makes it one of the most interesting string methods

print ' 123 '. Join (' abcdef ') print ' Hello '. Join (' | | | | ')

The result of the execution is:

A123b123c123d123e123f| hello| hello|

6. String Judging method



This article is from the "Raffaele" blog, make sure to keep this source http://raffaele.blog.51cto.com/6508076/1568586

Python Learning Note 1-python 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.