Several methods of string in python, python string

Source: Internet
Author: User

Several methods of string in python, python string

String formatting

>>> '%s plus %s equals %s' % (1,1,2)'1 plus 1 equals 2'

Field width and precision

* The field width is the minimum number of characters retained by the converted value, and the precision (for digits) is the number of decimal places included, or (for characters) the maximum number of characters that a converted value can contain.

>>> From math import pi >>>' % 10f' % pi '000000' >>> from math import pi >>>' % 10f' % pi # field width 10' 3.141593 '>>>' % 10.2f '% pi # The field width is 10, precision 2 '000000'> '%. 2f '% pi # accuracy 2' 3. 14'> '%. 5s '%' My name is ningsi 'My Na'>' %. * s '% (5, 'My name is ningsi')' My Na'

Symbol, alignment, and 0 Filling

>>> '% 010.2f' % pi # Fill '2017 with 0. 14'> '%-10.2f' % pi # Left alignment '3. 14' >>> print ('% 5d' % 10) + '\ n' + (' % 5d '%-10) 10-10 >>> print ('% + 5d' % 10) + '\ n' + (' % + 5d '%-10) + 10-10

 

String Method

Find

>>> N = 'ning si de shu de' >>> N. find ('de') 8 >>> N. find ('dee')-1 >>> N. find ('de', 9, 16) # The range contains the first index and does not contain the second-1

 

Join is the inverse method of the split method.

>>> s=['1','2','3','4']>>> q.join(s)'1+2+3+4'>>> p='','usr','bin','env'>>> '/'.join(p)'/usr/bin/env'>>> print 'C:'+'\\'.join(p)C:\usr\bin\env

Lower returns the lower-case string

>>> 'My name is ningsideshu'.lower()'my name is ningsideshu'>>> if 'name' in ['my','Name','is']:print 'Found it!'>>> if 'my' in ['my','Name','is']:print 'Found it!'Found it!

Replace

>>> 'This is a pen'.replace('pen','apple')'This is a apple'

Split splits strings into Sequences

>>> '1 + 2 + 3 + 4 + 5 '. split ('+') ['1', '2', '3', '4', '5']> '/usr/bin/env '. split ('/') ['', 'usr', 'bin', 'env']> 'Using the default '. split () # by default, all spaces are used as separators (spaces, line breaks, etc.) ['using', ''the, 'default']

Strip returns a string that removes spaces (or specified characters) on both sides (also: lstrip, rstrip)

>>> ' My name is Nsds  '.strip()'My name is Nsds'>>> ' *My name is Nsds * '.strip(' *')'My name is Nsds'

Replace translate. Unlike replace, a single character can be replaced (some parts of the string)

>>> From string import maketrans >>> N = maketrans ('ns', 'mf ') >>> 'My name is ningsideshu '. translate (N) 'My mame if mimgfidefhu '> 'My name is ningsideshu '. translate (N, 'M') # The second parameter specifies the characters to be deleted
'Y mame if mimgfidefhu'

Template string

>>> from string import Template>>> s=Template('$x. name $x!')>>> s.substitute(x='hello')'hello. name hello!'>>> s=Template("It't ${x}tastic!")>>> s.substitute(x='slurm')"It't slurmtastic!">>> s=Template("It't ${x}tastic${y}!")>>> s.substitute(x='slurm',y='a')"It't slurmtastica!">>> s=Template('A $thing must never $action.') >>> d={}>>> d['thing']='gentleman'>>> d['action']='show his socks'>>> s.substitute(d)'A gentleman must never show his socks.'

 

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.