Summary of common Python string operation functions [split (), join (), strip ()], pythonstrip

Source: Internet
Author: User

Summary of common Python string operation functions [split (), join (), strip ()], pythonstrip

This example describes common Python string operation functions. We will share this with you for your reference. The details are as follows:

Str. split ('')

1. Split by a certain character, such '.'

>>> s = ('www.google.com')>>> print(s)www.google.com>>> s.split('.')['www', 'google', 'com']

2. Split by a character and divide it n times. For example, split one time by '.'. The number of times the maxsplit bit is cut.

>>> s = 'www.google.com'>>> s'www.google.com'>>> s.split('.', maxsplit=1)['www', 'google.com']

3. Split by a string. For example, '|'

>>> s = 'WinXP||Win7||Win8||Win8.1'>>> s'WinXP||Win7||Win8||Win8.1'>>> s.split('||')['WinXP', 'Win7', 'Win8', 'Win8.1']>>>

''. Join (str)

Python hasjoin()Andos.path.join()The functions are as follows:

Join (): concatenate a string array. Concatenates string, tuples, and list elements with specified characters (delimiters) to generate a new string.
OS. path. join (): returns the result after combining multiple paths.

Operate the sequence (use ''and ':' as separators respectively)

>>> seq1 = ['hello','good','boy','doiido']>>> print ' '.join(seq1)hello good boy doiido>>> print ':'.join(seq1)hello:good:boy:doiido

Operate on strings

>>> seq2 = "hello good boy doiido">>> print ':'.join(seq2)h:e:l:l:o: :g:o:o:d: :b:o:y: :d:o:i:i:d:o

Operate on tuples

>>> seq3 = ('hello','good','boy','doiido')>>> print ':'.join(seq3)hello:good:boy:doiido

Operate dictionaries

>>> seq4 = {'hello':1,'good':2,'boy':3,'doiido':4}>>> print ':'.join(seq4)boy:good:doiido:hello

Merge Directories

>>> import os>>> os.path.join('/hello/','good/boy/','doiido')'/hello/good/boy/doiido'

Str. strip ()

Declaration: "s" is a string and "rm" is the sequence of characters to be deleted.

S. strip (rm) deletes the characters starting and ending in the s string and located in the rm deletion sequence;
S. lstrip (rm) Delete the characters starting from the s string and located in the rm Delete sequence;
S. rstrip (rm) deletes the characters at the end of the s string in the rm deletion sequence;

1. When rm is blank, blank spaces (including '\ n',' \ R', '\ t', '') are deleted by default ','')

For example:

>>> A = '123abc' >>> a. strip ('21') '3abc' returns the same result >>> a. strip ('12') '3abc'

2. Here, the rm Delete sequence is deleted as long as the characters on the edge (beginning or end) are in the delete sequence.

For example:

>>> A = '123abc' >>> a. strip ('21') '3abc' returns the same result >>> a. strip ('12') '3abc'

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.