Python Study Notes (3) Use strings

Source: Internet
Author: User

3.1 basic string operations


All standard sequence operations (index, sharding, multiplication, determining membership...) are also applicable to strings. However, please remember the wordsCharacter strings cannot be changed.


3.2 string formatting: Lite version


The first time I came into contact with python programming, I used a little bit of string formatting content. Here I just give an example to get a rough idea:

>>> format = " Hello,%s,%s enough for ya ?">>> values = ('world','hot')>>> print format % values Hello,world,hot enough for ya ?

3.3 string formatting: Full Version

Skip.


3.4 string method


There are many string methods (just like the string in C ++). Here we only introduce some particularly useful


3.4.1 find

Search for a substring in a long string. It returns the leftmost index of the substring's position. If not found,-1 is returned.

>>> 'With a moo-moo here,and a moo-moo there '.find('moo')7>>> title = "Monty Python"s Flying Circus">>> title.find('Monty')0>>> title.find('daxiao')-1

This method can also select optional start and end points.

>>> subject = '$$$ Get rich now !!! $$$'>>> subject.find('$$$')0>>> subject.find('$$$',1)21>>> subject.find('!!!',0,4)-1

3.4.2 join

The join method is a very important string method. It is the inverse method of the split method. It also adds elements to the queue.

>>> seq= [1,2,3,4,5]>>> sep= '+'>>> sep.join(seq)Traceback (most recent call last):  File "
 
  ", line 1, in 
  
       sep.join(seq)TypeError: sequence item 0: expected string, int found>>> seq=['1','2','3','4','5']>>> sep.join(seq)'1+2+3+4+5'
  
 
As you can see, all the queue elements to be added must be strings.


3.4.3 lower

The lower method returns the lower-case string.

>>> ' Hello ! World '.lower()' hello ! world '

3.4.4 replace

Returns the string after all matching items of a string are replaced.

>>> 'This is a test' .replace ('is','eez')'Theez eez a test'

3.4.5 split

This is a very important string method, which is the inverse method of the join method, used to split the string into sequences.

>>> '1+2+3+4+5'.split('+')['1', '2', '3', '4', '5']

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


3.4.6 strip

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

>>> '      internal whitespace is kept      '.strip()'internal whitespace is kept'

3.4.7 translate

The translate method is the same as the replace method. some parts of the string can be replaced. but unlike the former. the translate method can only process a single character. its advantages allow multiple replicas at the same time, which is sometimes much more efficient than replacee.

Before using translate for conversion, you must complete a conversion table. Here you can use the maketrans function in the string module.

The maketrans function requires two parameters: two equal-length strings, indicating that each character in the first string is replaced by a character in the same position in the second string.


>>> from string import maketrans>>> table = maketrans ('cs','kz')>>> ' this is an incredible test '.translate(table)' thiz iz an inkredible tezt '

The second parameter of trans is optional. this parameter is used to specify the characters to be deleted.

>>> 'this is an incredible test'.translate(table ,' ')'thizizaninkredibletezt'


Summary

String formatting:

String Method

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.