The third lesson of the basic Python tutorial 0121

Source: Internet
Author: User
Tags string methods

Learn Python lesson three today, using strings. (All standard sequence Operations "index, Shard, multiply, Judge membership, seek length, take minimum and maximum value" are also applicable to strings)

1.% (conversion specifier) usage,% marks the position where the converted value needs to be inserted.

>>> formkk= "Hello,%s.%s enough for?" Note: Use% to insert strings sequentially into the original sentence.
>>> value= (' World ', ' egg ')
>>> Print Formkk%value
Hello, world. Egg enough for?
>>>

Note: To output%, you need to use percent.

2. Format the output floating-point number. (The printf () output function of analogy C)

>>> forma= "Pi with three decimals:%.3f"//Three decimal places after decimal point
>>> from Math import pi
>>> Print forma% pi
Pi with three decimals:3.142
>>>

3. Simple Introduction to template strings

The substitute template method replaces the corresponding $ in the string with the keyword argument passed in.

>>> from string import Template
>>> s=template (' $x. Glorious $x! ')
>>> S.substitute (x= ' ssssss ')
' Ssssss.glorious ssssss! '
>>>

If the replacement word is part of a word, then the parameter name must be enclosed in parentheses.

>>> from string import Template
>>> s=template ("It ' s ${x}tastic!")
>>> S.substitute (x= ' fan ')
"It ' s fantastic!"
>>>

4. String methods

Find can look up substrings in a longer string. It returns the leftmost index of the location where the substring is located. Returns 1 if it is not found.

>>> ' with a moo-moo here, and a moo-moo there '. Find (' moo ')
7
>>> title= "Money,money"
>>> title.find (' money ')
0
>>> title.find (' m ')
0
>>> Title.find (' n ')
2
>>> title.find (' t ')
-1
>>>

You can also set the starting and ending positions to find

>>> sub= ' $$ get Rich now! $
>>> sub.find (' $$ ')
0
>>> sub.find (' $$ ', 1,6)
-1
>>> sub.find (' $$ ', 0,6)
0
>>> sub.find (' $ ', 0,17)
0
>>>

The join method, the inverse of the split method, is used to concatenate elements in the string.

>>> b=[' A ', ' B ', ' C ']

>>> s= ' + '
>>> S.join (b)
' A+b+c '
>>> dirs= ' C: ', ' usr ', ' bin ', ' Eny '
>>> '/'. Join (dirs)
' C:/usr/bin/eny '
>>> print ' D: ' + ' \ \ '. Join (dirs)
D:c:\usr\bin\eny
>>>

Lower method that returns the lowercase master in a string

>>> ' The older fisherman has a CAT '. Lower ()
' The older fisherman has a cat '

>>> name= ' RR '
>>> name2=[' rr ', ' Dubi ']
>>> if Name.lower () in Name2:print ' found! '
found!
>>>

The Replace method returns a string after all occurrences of a string have been replaced

>>> ' The older fisherman has a CAT '. Replace (' older ', ' younger ')
' The younger fisherman has a CAT '
>>> ' The older fisherman has a CAT '. Replace (' s ', ' 88 ')
' The older Fi88herman ha88 a CAT '
>>>

The split method is the inverse of join, which is used to split the string into sequences

>>> ' 1+2+3+4 '. Split (' + ')
[' 1 ', ' 2 ', ' 3 ', ' 4 ']
>>> ' using the Split method '. Split ()
[' Using ', ' the ', ' Split ', ' method ']
>>>

< Span style= "color: #000000; font-size:15px ">< Span style= "color: #0000ff; Font-size:18px The >translate method replaces some parts of a string, unlike replace, translate processes only a single character. The advantage is that multiple substitutions can be made at the same time, sometimes more efficient than replace

>>> from string import Maketrans
>>> Table=maketrans (' cs ', ' KZ ')//replace the character C with K and replace S with Z
>>> len (table)
256
>>> table[97:123]
' Abkdefghijklmnopqrztuvwxyz '
>>> ' This was an incredible test '. Translate (table)
' Thiz iz an inkredible tezt '
>>> Table=maketrans (' s ', ' 8 ')
>>> ' This was an incredible test '. Translate (table)
' Thi8 i8 an incredible te8t '
>>>

Summary, before you can use the translate conversion, you need to complete a conversion table in which the corresponding relationship of a character is replaced with a character. You can use the Maketrans function in the string template.

< Span style= "color: #000000; font-size:15px ">< Span style= "color: #000000; font-size:15px "> The Strip method returns a string that strips both sides (excluding interior) spaces

>>> ' I am a gril '. Strip ()
' I am a gril '
>>> names=[' Gumby ', ' dooou ', ' Yullo ']
>>> name= ' Gumb y '
>>> if Name.strip () in Names:print ' found! '

>>> n= ' Gumby '
>>> if N.strip () in Names:print ' found! '

found!
>>>

You can also specify the characters to be removed (removing characters from both sides) in fact, these methods are often used when dealing with dirty data.

>>> uu= '%%%he%llo% '
>>> uu.strip ('% ')
' He%llo '
>>>

The third lesson of the basic Python tutorial 0121

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.