Common examples herein describes Python string manipulation functions. Share to you for your reference, as follows:
str.split ( '')
1. Press the segmentation of a single character, such as '.'
>>> s = ('www.google.com')
>>> print(s)
www.google.com
>>> s.split('.')
['www', 'google', 'com']
2. Press one character split and divided n times. The division by 1 ''; cutting bit number parameter maxsplit
>>> s = 'www.google.com'
>>> s
'www.google.com'
>>> s.split('.', maxsplit=1)
['www', 'google.com']
3. segmented by a string. Such as: '||'
>>> s = 'WinXP||Win7||Win8||Win8.1'
>>> s
'WinXP||Win7||Win8||Win8.1'
>>> s.split('||')
['WinXP', 'Win7', 'Win8', 'Win8.1']
>>>
’ '.join(str)
Python, the join () and the os.path.join () two functions, specifically effects include:
join (): connecting string array. A string, a tuple, in the list element to specify character (separator) connected to generate a new string
os.path.join (): returns the plurality of combinations of paths
The operation sequence (respectively using 'and': 'as a separator)
>>> seq1 = ['hello','good','boy','doiido']
>>> print ' '.join(seq1)
hello good boy doiido
>>> print ':'.join(seq1)
hello:good:boy:doiido
Manipulate 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
Operating a tuple
>>> seq3 = ('hello','good','boy','doiido')
>>> print ':'.join(seq3)
hello:good:boy:doiido
Operating a tuple
>>> seq3 = ('hello','good','boy','doiido')
>>> print ':'.join(seq3)
hello:good:boy:doiido
To operate dictionary
>>> seq4 = {'hello':1,'good':2,'boy':3,'doiido':4}
>>> print ':'.join(seq4)
boy:good:doiido:hello
The combined catalog
>>> import os
>>> os.path.join('/hello/','good/boy/','doiido')
'/hello/good/boy/doiido'
str.strip()
Disclaimer: s a string, rm is a sequence of characters to be deleted
s.strip (rm) to delete the beginning of the string s, at the end, located rm delete character sequence;
s.lstrip (rm) to delete the beginning of the string s, rm located delete character sequence;
s.rstrip (rm) at the end of the string s delete, delete character sequence rm located;
- When rm is empty, the default deletion whitespace (including '\ n', '\ r', '\ t', '')
E.g:
>>> a = '123abc'
>>> a.strip('21')
'3abc' 结果是一样的
>>> a.strip('12')
'3abc'
2. Here is the sequence rm delete characters on the long side (beginning or end) in the deletion sequence, it is deleted.
E.g:
>>> a = '123abc'
>>> a.strip('21')
'3abc' 结果是一样的
>>> a.strip('12')
'3abc'
We recommend the python learning sites to see how seniors are learning! From basic python script, reptiles, django, data mining, programming techniques, as well as to combat zero-based sorting data items, given to every love learning python small partner! Python veteran day have to explain the timing of technology, to share some of the ways to learn and need to pay attention to small details, click on Join us python learner gathering