Python BASICS (4) -- strings

Source: Internet
Author: User
Tags string methods

All standard sequence operations apply to strings, but strings are immutable.

Character % is used for string formatting:

Put a string on the left side of %, and put the value to be formatted on the right side. You can use one value or the tuples or dictionaries of multiple values.

>>> format=>>> values=(,>>>  format % ya?

If the tuples to be converted exist as part of the conversion expression, you must enclose them in parentheses to avoid errors.

String Method

Strings are much richer than list methods, because strings are inherited from the string module. This article only introduces some particularly useful string methods.

 1. find

The find method can be used to search for a substring in a long string. It returns the leftmost index of the substring location. If no substring is found,-1 is returned.

>>> .find(7>>> title=>>> title.find(>>> title.find(6>>> title.find(-1

This method can accept the optional start and end parameters:

>>> subject=>>> subject.find(>>> subject.find(,120>>> subject.find(16>>> subject.find(,0,16-1

2. join

The join method is a very important string method. It is the inverse method of the split method, used to add elements to the queue:

>>> seq=[1,2,3,4,5>>> sep=>>>>>> seq=[,,,,>>>>>> dirs=,,,>>> >>>  +

3. lower

The lower method returns the lower-case string.

>>> 

4. replace

The replace method returns the string after all matching items of a string are replaced.

>>> .replace(,

5. split

It is the inverse method of join, used to split strings into Sequences

>>> .split(, , , , >>> .split(>>> , , ]

NOTE: If no separator is provided, the program uses all spaces as separators.

6. strip

The strip method returns a string that removes spaces on both sides (excluding internal spaces:

>>> 

You can also specify the characters to be removed and column them as parameters:

>>> .strip(

Note: Only the characters on both sides are removed.

7. translate

The translate method is the same as the replace method. Some parts of the string can be replaced. However, unlike the former method, the translate method only processes a single character.

 

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.