String processing in python

Source: Internet
Author: User

1. String Conversion

S. lower () to lowercase

S. upper () is converted to uppercase

S. swapcase () in upper case and lower case in upper case

S. capitalize () is capitalized.

Convert to int type string. atoi (s) or int (s)

Convert to float type string. atof (s) or float (s)

Convert to long type string. atol (s) or long (s)

2. Search and other operations

S. find (sub, [, start [, end]) returns the position where the first appearance occurs. If no position is found,-1 is returned.

S. rfind (sub, [, start [, end]) returns the position of the last occurrence. If no result is found,-1 is returned.

S. index (sub [, start [, end]) is similar to find (). If no value is found, ValueEerror is returned.

S. rindex (sub [, start [, end]) is similar to rfind (). If no value is found, ValueError is returned.

S. count (sub [, start [, end]) returns the number of times that a substring appears.

S. replace (old, new [, maxreplace]) replaces the string. When maxreplace is specified, only the first maxreplace is replaced.

S. strip (char) deletes the char at the beginning and end

S. split ([, seq [, maxsplit]) returns the list of split strings.

S. join ([sep]) connection string

3. Location

S. ljust (width [, fillchar]) left alignment

S. Align ust (width [, fillchar]) right alignment

S. center (width [, fillchar]) center

S. zfill (width) left fill zero until length to width

4. format the output

Format can change the output format of a string, for example:

'{0}, {2}, {1}'. format ('A', 'B', 'C ')

Here {0} {1} {2} respectively refer to 'a 'B'

You can also write it by name:

'Cordix: {x}, {y} '. format (x = '1', y = '2 ')

You can also use format to align the left of a string.

'{: <10}'. format ("hello") is left aligned and the width is 10.

'{:> 10}'. format ("hello") right alignment, width: 10

'{: ^ 10}'. format ("hello") center, width: 10

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.